9

Can you tell me how to access a variable of a script from another script ? I have even read everything in unity website but I still can’t do it. I know how to access another object but not another variable.

This is the situation : I’m in script B and I want to access the variable X from script A. The variable X is boolean. Can you help me ?

Btw i need to update X’s value costantly in script B , how do I do that ? Access it in Update function If you could give me and example with these letters would be great !

Thank you

DarkCygnus
  • 7,420
  • 4
  • 36
  • 59
Lorenzo Spoleti
  • 111
  • 1
  • 1
  • 5

3 Answers3

18

You first need to get the script component of the variable, and if they're in different game objects, you'll need to pass the Game Object as a reference in the inspector.

For example, I have scriptA.cs in GameObject A and scriptB.cs in GameObject B:

scriptA.cs

// make sure its type is public so you can access it later on
public bool X = false;

scriptB.cs

public GameObject a; // you will need this if scriptB is in another GameObject
                     // if not, you can omit this
                     // you'll realize in the inspector a field GameObject will appear
                     // assign it just by dragging the game object there
public scriptA script; // this will be the container of the script

void Start(){
    // first you need to get the script component from game object A
    // getComponent can get any components, rigidbody, collider, etc from a game object
    // giving it <scriptA> meaning you want to get a component with type scriptA
    // note that if your script is not from another game object, you don't need "a."
    // script = a.gameObject.getComponent<scriptA>(); <-- this is a bit wrong, thanks to user2320445 for spotting that
    // don't need .gameObject because a itself is already a gameObject
    script = a.getComponent<scriptA>();
}

void Update(){
    // and you can access the variable like this
    // even modifying it works
    script.X = true;
}
Jay Kazama
  • 3,167
  • 2
  • 19
  • 25
  • Should the way to acces `X` be with `script.X = true;` rather than `scriptA.X = true;` ? You can't access `X` that way if it is not `static` – DarkCygnus May 30 '16 at 17:02
  • 1
    @GrayCygnus whoops u spotted my mistake. Thanks, I have fixed it. – Jay Kazama May 31 '16 at 09:46
  • If you reference the GameObject `a` anyway you could also simply reference the wanted component directly without having to call `GetComponent` – derHugo Oct 10 '18 at 05:05
1

just for completing the first answer

there is no need for

a.gameObject.getComponent<scriptA>();

a is already a GameObject so this will do

a.getComponent<scriptA>();

and if the variable you are trying to access is in children of the GameObject you should use

a.GetComponentInChildren<scriptA>();

and if you need a variable of it or method you can access it like this

a.GetComponentInChildren<scriptA>().nameofyourvar;
a.GetComponentInChildren<scriptA>().nameofyourmethod(Methodparams);
derHugo
  • 83,094
  • 9
  • 75
  • 115
Milad Qasemi
  • 3,011
  • 3
  • 13
  • 17
1

You can use static here.

here is the example:

ScriptA.cs

Class ScriptA : MonoBehaviour{
 public static bool X = false;
}

ScriptB.cs

Class ScriptB : MonoBehaviour{
 void Update() {
   bool AccesingX = ScriptA.X;
   // or you can do this also 
   ScriptA.X = true;
 }
}

OR

ScriptA.cs

Class ScriptA : MonoBehaviour{

//you are actually creating instance of this class to access variable.
 public static ScriptA instance;

 void Awake(){

     // give reference to created object.
     instance = this;

 }

 // by this way you can access non-static members also.
 public bool X = false;


}

ScriptB.cs

Class ScriptB : MonoBehaviour{
 void Update() {
   bool AccesingX = ScriptA.instance.X;
   // or you can do this also 
   ScriptA.instance.X = true;
 }
}

for more detail, you can refer singleton class.

Kartik Shah
  • 143
  • 2
  • 5
  • 14