- Home /
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.
PS. I specifically do NOT want to use a static variable. Hence this method.
Answer by Statement · Apr 04, 2012 at 10:34 AM
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.
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 TYV$$anonymous$$!