0

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 constantly in script B, how do I do that? Access it in Update function If you could give me an example with these letters would be great!

Thank you

derHugo
  • 83,094
  • 9
  • 75
  • 115
Dranreb
  • 93
  • 1
  • 1
  • 10

1 Answers1

0

Your question is answered here:

https://answers.unity.com/questions/7555/how-do-i-call-a-function-in-another-gameobjects-sc.html

Basically, you create a variable of type "ScriptA" inside script B and put the gameobject containing script A inside the value field of that variable. Then you can access all public functions and variables of script A in script B.

Ahorn
  • 649
  • 4
  • 19
  • Thanks! I'm a bit naive in scripting yet. Can you provide a very basic/simple example? – Dranreb Aug 14 '18 at 05:57
  • I currently don't have unity installed on my system, but if you follow the code samples provided in the link i gave, you should see a field "ScriptA" in the inspector of the gameObject containing the script B. You then create an empty game object containing script A (or use the gameObject that already contains script A) and simply drag and drop it into the field. – Ahorn Aug 14 '18 at 06:23