|
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".
(comments are locked)
|
|
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?
Dec 27 '11 at 06:31 PM
jister
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.
Jun 27 '12 at 07:30 PM
MP0732
> you cannot have two different scripts instantiate the same prefab at the same time. Actually there are no problems doing that.
Jun 27 '12 at 07:35 PM
Eric5h5
(comments are locked)
|
|
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.
Dec 15 '10 at 02:13 PM
Statement ♦♦
(comments are locked)
|
|
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(); Thanks Patrick :)
Jul 25 '11 at 05:43 PM
defaxed
(comments are locked)
|

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....