- Home /
Problem: Accessing Other Script Variables
Hello :)
There is a small problem I have when I try to access a public variable from another script.
I have a Ghost prefab, and it's children are the left and right arm. When the Ghost is instantiated, it's children (the arms) produce results for some variables and the parent (the ghost) is mean to read them. However this is not happening.
Here is part of the arm script:
public var forward_left: boolean;
public var visible_left: boolean;
function Start () {
}
function Update () {
if(Physics.Raycast(transform.position,
(GameObject.Find("MainChar").transform.position - transform.position).normalized,
Vector3.Distance(transform.position, GameObject.Find("MainChar").transform.position)-1)){
visible_left = false;
}else{visible_left = true;}
if(Physics.Raycast(transform.position, transform.forward, 1)){
forward_left = true;
} else{forward_left = false;}
}
This is part of my ghost script:
if(GameObject.Find("left").GetComponent(ARM_left).forward_left == true){do_something;}
Am I doing something wrong? Does "GameObject.Find" NOT look for children?
Thanks in advance :D Muhasaresa ---||---
Answer by valaxgames · Dec 01, 2012 at 05:19 PM
You need to add 'static' before the variable you want to access. For example:
static var health : int = 100;
To access it:
//Will print the health variable in the other script.
Debug.Log(ScriptName.health);
Hope I helped!
You do not need to add "static", and should not unless you actually mean for there to be one instance of that variable.
Answer by neogrant2 · Dec 01, 2012 at 05:19 PM
Assuming your "Arm script" is called ArmScript.js,
Change:
static var forward_left : boolean = false;
static var visible_left : boolean = false;
In your Ghost script:
if(ArmScript.forward_left){
print("True");
}
Don't use static except for specific circumstances. It's not a substitute for GetComponent.
Answer by Eric5h5 · Dec 01, 2012 at 05:38 PM
GameObject.Find does find children, however you should use Transform.Find.
Your answer
Follow this Question
Related Questions
Accessing variable from another script 1 Answer
Enemy Health Damage 2 Answers
Cloning Problem Script 0 Answers
Accessing not yet publicly added scirpt 3 Answers
Platform Mover continued 1 Answer