Quantcast
Channel: Answers for "The object of type 'Rigidbody' has been destroyed but you are still trying to access it."
Viewing all articles
Browse latest Browse all 4

Answer by drak0

$
0
0
I don't have experiente with C# but from what I see, you are trying to acces the same object that you destroy in the Projectile script.Try this : declare a new variable to hold a reference of the instantiated projectile ( you are using the same identifier, "projectile" for the projectile prefab that you instantiate, and also for the variable that holds the reference to that new instance).
public class MissileLauncher : MonoBehaviour
{
    public Rigidbody projectile;
	public Rigidbody spawnedProjectile;
    int speed = 50;

    void Update ()
    {
        if (Input.GetButtonDown("Fire1"))
        { 
            spawnedProjectile = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation); //Instantiate projectile
            spawnedProjectile.velocity = transform.TransformDirection(new Vector3(0,0,speed)); //Set projectile velocity

            Physics.IgnoreCollision(spawnedProjectile.collider, transform.root.collider); //Avoid projectile an hero
        }
    }
}
This should work for you.

Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles



Latest Images