- Home /
"Cannot cast from source type to destination type"- instantiating
i've seen a few questions asked that are related to this, but none work, please help me figure out my problem, I have an AI that shoots bullets, but I get the error: "Cannot cast from source type to destination type".
var projectile : Rigidbody; var attackTimer : float = 1; var coolDown : float = 1;
function Shoot () {
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(attackTimer == 0){
var clone : Rigidbody;
//this is where it shows the error...
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection (Vector3.forward * 10);
attackTimer = coolDown;
}
}
I am not a JS coder so I dont know about all the subtle differences but can you try to cast the result from Instantiate to Rigidbody type? Such as (Rigidbody)Instantiate(pro....
Answer by MonkeyAssassin8 · Dec 16, 2010 at 12:50 PM
I figured it out and my problem was that I had another script which activated the shoot script, once I merged it into one script it worked perfectly
sorry to bump this, but could be more clear on what was wrong?
I had the same problem. What I think he means to say is that you cannot have two different scripts instantiate the same prefab at the same time.
Once I copied the prefab and renamed and assigned the copy to the second script, both scripts worked fine.
Here's my own, second script, generalized, which was in this area of the script identical to the first:
var projectilePrefab : Rigidbody;
function ShootProjectile () { var newProjectile : Rigidbody = Instantiate(projectilePrefab, transform.position, transform.rotation); newProjectile.transform.Rotate(90, 0, 0); newProjectile.name = "projectile"; newProjectile.AddForce(newProjectile.transform.up * 500); Physics.IgnoreCollision(transform.root.collider, newProjectile.collider, true);
I simply assigned a copy of the original prefab to the variable "projectilePrefab", and the script worked fine.
you cannot have two different scripts instantiate the same prefab at the same time.
Actually there are no problems doing that.
Answer by Patrick Lynch · Apr 24, 2011 at 04:57 AM
You've got to cast it to a transform, then you can get the gameObject or any other components.
Transform g = (Transform) Instantiate(indicatorPrefab, new Vector3(0,0,0), Quaternion.identity); GameObject obj = g.gameObject; GUITexture gui = g.gameObject.GetComponent();
Answer by bateleur · Dec 15, 2010 at 02:05 PM
Assuming you're sure that 'projectile' is not null, it appears you might have encountered an instance of the problem discussed here:
http://forum.unity3d.com/threads/1726-Instantiate-and-type-cast-errors
If so, as it recommends, restarting Unity should fix it. (Where by "fix" I mean that it will stop happening for now but may recur later.)
That "fix" just sounds like you're recompiling the scripts. If the script had an error in the previous build and for some reason did not pick up the changes to the file (which happen frequently), the old build would still be in effect along with any errors they caused. There should be no need to restart unity but to simply reimport the script. I may of course be mistaken, but I find it highly reasonable this was the case.
Answer by gw1108 · Jan 20, 2014 at 11:11 AM
Instantiate returns type of object which is the base class for all objects. http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
I usually cast to game object then get the component, or in your case rigidbody, instead. However, to keep your code you would need to cast (Rigidbody)Instantiate().
Your answer
Follow this Question
Related Questions
InvalidCastException: Cannot cast from source type to destination type. 3 Answers
InvalidCastException: Cannot cast from source type to destination type. 4 Answers
unable to cast from source to destination 0 Answers
Cannot cast from source to destination type 1 Answer
"Cannot cast from source type to destination type" 2 Answers