x


Accessing variable of script on an instance.

var chutist : GameObject;
var chutistClone : GameObject;

function Start () {

    chutistClone = Instantiate(chutist, Vector3(transform.position.x,transform.position.y -0.85, transform.position.z),transform.rotation);

}


function OnTriggerEnter(Col : Collider){

 if (Col.gameObject.tag == "Bullet"){

    life -= 10;
 }

 if (life == 0){

    chutistClone.rigidbody.drag = 0;
    chutistClone.GetComponent(chutist).drop = true;
    yield WaitForSeconds (0.01);
    destruct();
 }



This is about the part "chutistClone.GetComponent(chutist).drop = true;"

I've checked out various methods of accessing scripts on other objects, But I can't figure out why this does not work. Unity gives me:

BCE0023: No appropriate version of 'UnityEngine.GameObject.GetComponent' for the argument list '(UnityEngine.GameObject)' was found.

If I change (chutist) to ("chutist") it gives me:

BCE0019: 'drop' is not a member of 'UnityEngine.Component'.


What am I missing? What am I doing wrong?

Thanks in advance, Bor.

more ▼

asked Apr 04 '12 at 10:00 AM

ManiacalSquare gravatar image

ManiacalSquare
4 2 2 3

PS. I specifically do NOT want to use a static variable. Hence this method.

Apr 04 '12 at 10:28 AM ManiacalSquare
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

The problem is that you are sending a game object into a function that expect a type.

var chutist : GameObject;
...
chutistClone.GetComponent(chutist).drop = true;

I would assume you meant to do something similar to this:

chutistClone.GetComponent.<ChutistScript>().drop = true;

But I don't know the type of the component you are trying to access.
Change ChutistScript to the type of component that has the drop variable.

If you get a null reference exception after you do this, it likely means that you don't have the script on the object you cloned.

more ▼

answered Apr 04 '12 at 10:34 AM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

I was really stupid. Everything was fine, except the variable "chutist " was named the same as the script "chutist". So it was trying to access the wrong thing. Changed the variable name to chutist1, everything is working fine now. Sorry for wasting your time guys. Thanks to your answer I figured it out. So TYVM!

Apr 04 '12 at 12:20 PM ManiacalSquare
(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:

x3344
x826
x204
x47

asked: Apr 04 '12 at 10:00 AM

Seen: 951 times

Last Updated: Apr 04 '12 at 12:20 PM