- Home /
Access to variables from other script objects
Hi I just want to change and display some variables in my player class.
Here is my player class:
public class SpielerController : MonoBehaviour {
public int SpielerPunktzahl;
public string SpielerName;
// Use this for initialization
void Start () {
this.SpielerPunktzahl = 24;
}
// Update is called once per frame
void Update () {
}
}
The GameObject is called "Player1" where this script is attached.
Now I'm trying to get access to these two variables from another GameObjact called "GameGUI". How can i get access from within the script attached to the GameGUI object?
Would be nice if you could help me to solve this problem. I have searched a lot and tried a lot examples from the internet, but nothing worked.
Thaks a lot
Answer by Esa · Feb 17, 2012 at 06:16 AM
Unity Script reference is your friend: Accessing Other Game Objects.
This link does not work anymore. Accessing Other Game Objects.
Here's a very good answer on accessing variables from other GameObjects
Answer by Dejan1 · May 09, 2013 at 11:49 PM
Just make - public static int SpielerPunktzahl; and you can directly acces and change this variable in another script directly typing the originalscriptname.SpielerPunktzahl=somenumber;
Answer by allenziff · Dec 05, 2012 at 08:14 PM
type this in the top:
public static SpielerController instance;
then in the void Start() type in:
instance = this;
then on the other script type in were needed:
SpielerController.instance.yourstuff = 12/false/whatever;
Your answer
Follow this Question
Related Questions
Can I access components scripts? 3 Answers
Add material component to object script-wise 4 Answers
A node in a childnode? 1 Answer
Painted Tree's Script Component 1 Answer
Unity crashes when accessing a function of a script from another script 2 Answers