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

3 Answers

3

Unity Script reference is your friend: Accessing Other Game Objects.

This link does not work anymore. [Accessing Other Game Objects.][1] [1]: http://docs-jp.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

Here's a very good answer on [accessing variables from other GameObjects][1] [1]: http://answers.unity3d.com/questions/11527/how-do-i-access-information-eg-variables-functions.html

Just make - public static int SpielerPunktzahl;
and you can directly acces and change this variable in another script directly typing the originalscriptname.SpielerPunktzahl=somenumber;

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;