- Home /
Cast Source Instantiating Error
Unfortunately I have run around again now with an odd casting source error that I cannot seem to resolve. Basically I have a rocket (instantiated) that should explode (instantiate an explosion) upon contact with its collider.
Right now the collider IS set as a trigger, and the explosion is a prefab. This is the exact error that I am getting:
InvalidCastException: Cannot cast from source type to destination type.
RocketBlowup.OnTriggerEnter (UnityEngine.Collider other) (at Assets/MainFolder/Scripts/RocketBlowup.js:5)
The code I am using is a simple "On trigger enter" function which is attached to the rocket. The error seems to be pointing directly to the instantiation line of my code - which is completely correct I believe:
var explosion : Transform;
var blowuppoint : Transform;
function OnTriggerEnter (other: Collider) {
var expl : Rigidbody;
expl = Instantiate(explosion, blowuppoint.position, blowuppoint.rotation);
Destroy(gameObject);
}
The "blowuppoint" is simply an empty gameobject positioned behind the rocket so that the point of explosion is in the right place.
Thanks in advance, CarbonTech Software Admin
Answer by StephanK · Jun 28, 2010 at 10:34 PM
The explosion you are trying to instantiate is a Transform. You are trying to assign the Transform to a variable of type Rigidbody. Try Instantiate().rigidbody instead. Or change the expl type to Transform.
Thanks! I didn't expect such a fast response to my question ;)
Your answer
Follow this Question
Related Questions
Instantiate Clones Itself - Rather than Prefab 0 Answers
Instantiate Not Working - Boo Script 1 Answer
Cloned Object does not run Scripts 2 Answers
i attach the prefab, but when i hit play, its not the prefab anymore 0 Answers
Deleting a GameObject 2 Answers