So basically, lets say i have a cannon, i wanted to make it shoot a cannonball(prefabs) and leave an explosion(prefab) at the place it collided.
example code for projectile:
using UnityEngine;
public class projectiles : MonoBehaviour
{
public int life;
cannonball cannonball
private void Awake()
{
Destroy(gameObject, life);
}
void OnCollisionEnter(Collision collision)
{
GameObject endeffect = Instantiate(explosion, body.position, body.rotation, spells);
endeffectrb.transform.localscale*= cannonball.size;
Destroy(gameObject);
}
}
'''
example code for explosion:
'''
using UnityEngine;
public class cannonball: MonoBehaviour
{
public GameObject ball;
public Transform player;
public Transform cam;
public KeyCode shootkey = KeyCode.Mouse0;
public float speed = 10;
public float size= 10f;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(shootkey))
{
shoot();
}
}
public void shoot()
{
player.transform.localPosition = new Vector3(1 * size / 10, 1 * size / 10, 1 * size / 10);
GameObject projectile = Instantiate(ball, player.position, cam.rotation);
projectile.transform.localScale *= size / 10;
Rigidbody ballrb = projectile.AddComponent<Rigidbody>();
ballrb.useGravity = false;
ballrb.AddForce(cam.rotation * Vector3.fwd * speed, ForceMode.Force);
}
}
But however after i instantiate the cannonballs from the cannon, I cant get it to instantiate an explosion as well as being unable to access the explosion gameobject and change its properties. I spent a few hours trying to solve this but all the resources i found did not mention anything about instantiating from a prefab so pls help.
this is whats going on so far
That's enough to find the problem and fix it, or ask a good question.