- Home /
The object of type 'Rigidbody' has been destroyed but you are still trying to access it.
I have a basic missile launcher set up. The collision works fine, the explosion animation plays when the missile collides with something, and then it gets destroyed. The only problem is, I can't shoot any more missiles after the first one!
I have a prefab named "missile" with a rigidbody attached, which is associated with the projectile variable from MissileLauncher. Is there another way to instantiate a new projectile after destroying the previous gameObject?
public class MissileLauncher : MonoBehaviour
{
public Rigidbody projectile;
int speed = 20;
void Update ()
{
if (Input.GetButtonDown("Fire1"))
{
projectile = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation); //Instantiate projectile
projectile.velocity = transform.TransformDirection(new Vector3(0,0,speed)); //Set projectile velocity
Physics.IgnoreCollision(projectile.collider, transform.root.collider); //Avoid projectile an hero
}
}
}
public class Projectile : MonoBehaviour
{
public GameObject explosion;
void OnCollisionEnter(Collision collision)
{
ContactPoint contact = collision.contacts[0];
transform.rotation = Quaternion.FromToRotation (Vector3.up, contact.normal);
explosion = (GameObject)Instantiate(explosion, contact.point, transform.rotation);
Destroy(gameObject);
}
}
public class Explosion : MonoBehaviour
{
float explosionTime = 0.25f;
void Start ()
{
Destroy (gameObject, explosionTime);
}
}
Everything seems to be in order as far as I get it, since I only work with javascript. Besides that, you should be able to virtually instantiate infinite copies of a prefab. Can you instantiate more than one prefab at a time before the first one exploding? Also, how are you trying to tell the hero that it's ok to shoot again?
@sevenrmn:
He uses GetButtonDown, so the player can shoot as fast as he can press the button (which is only limited by the frames per second) ;)
@Bunny83 $$anonymous$$y question was actually answered by your answer, as he can't instantiate more than one because he is disposing of the prefab variable. I'll have to get more familiar with C# again so I can pinpoint these kinds of details.
Answer by Bunny83 · Aug 07, 2012 at 12:32 AM
Instantiate will return the object it instantiates. You overwrite your original prefab reference with your instance. When this instance get destroyed you don't have any source object you can Instantiate.
So, just do it like this:
// [...]
if (Input.GetButtonDown("Fire1"))
{
Rigidbody clone = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(new Vector3(0,0,speed));
Physics.IgnoreCollision(clone.collider, transform.root.collider);
}
// [...]
Answer by drak0 · Aug 07, 2012 at 01:07 AM
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.