- Home /
Invalid Cast Exception
Looking at the related tags I've had no luck getting this instantiate code fixed.
var projectile : Rigidbody;
and at some point, the Shoot function gets called.
function Shoot()
{
var instantiatedProjectile : Rigidbody;
instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity = transform.TransformDirection ( Vector3( 0,0, 18));
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
lastShot = Time.time;
timesShot +=1;
}
According to unity, the problem is in this line:
instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation );
projectile and instantiateProjectile are both Rigidbody. There shouldn't be anything to change here. What am I missing?
Worth noting: I copy and pasted the instantiate code from another script and both the code, the prefab (projectile) and the world is nice and dandy. For some reason when I'm trying to reuse the code/prefab I'm getting this.
And the Prefab shooting the projectile also destroys itself with the shots. Even though I'm reusing the IgnoreCollision line.
Answer by CHPedersen · Apr 12, 2011 at 10:44 AM
This is a shot in the dark, but did you make sure to drag'n'drop the prefab onto the variable "projectile" in the editor? If not, Unity won't know which prefab to instantiate a copy of at runtime.
Oh sweet Jesus. This was it, but I swear I've checked it before. It wouldn't have been the first time it happened to me. I had "ShotBaddieShot" (the right one) in the editor but it had the Transform Icon, not the Rigidbody. I swear I replaced it a few times trying to get it to work. Ugh, I got back to my comptuer and you made it work. Thank you.