x


"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;
    }
}
more ▼

asked Dec 15 '10 at 01:23 PM

MonkeyAssassin8 gravatar image

MonkeyAssassin8
271 35 38 43

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

Dec 15 '10 at 02:15 PM Statement ♦♦
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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

more ▼

answered Dec 16 '10 at 12:50 PM

MonkeyAssassin8 gravatar image

MonkeyAssassin8
271 35 38 43

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)
10|3000 characters needed characters left

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

more ▼

answered Dec 15 '10 at 02:05 PM

bateleur gravatar image

bateleur
38 3 5 10

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)
10|3000 characters needed characters left

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();

more ▼

answered Apr 24 '11 at 04:57 AM

Patrick Lynch gravatar image

Patrick Lynch
21 1

Thanks Patrick :)

Jul 25 '11 at 05:43 PM defaxed
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x132
x69
x54
x37
x18

asked: Dec 15 '10 at 01:23 PM

Seen: 7079 times

Last Updated: Jun 27 '12 at 07:35 PM