티스토리 뷰

I show How to refer to a variable in other files.

(refer to a variable of class being componentization)


2 GameObjects, 2 files Each objects have.


How to refer to the value of speed variable in GameManager class.


1.  In Source code


public class GameManager : MonoBehaviour {

PlayerControl pc;


void Start () {

GameObject obj = GameObject.Find ("Player");

if (obj != null)

pc = obj.GetComponent<PlayerControl>();


Debug.Log ("speed : " + pc.speed);

}

void Update () {


}

}


2. in Unity Editor

1) Variable definition of PlayerControl class type

public class GameManager : MonoBehaviour {

public PlayerControl pc;


void Start () {

Debug.Log ("speed : " + pc.speed);

}

void Update () {


}

}


2)

you play the game, you cans see  an error like below

You have to connect reference variable of "pc".


NullReferenceException: Object reference not set to an instance of an object
GameManager.Start () (at Assets/Scripts/GameManager.cs:8)


Choose GUI Obejct, drag and drop over the variable "pc" of  GameManager component.


and then you use the variable of "speed" of PlayerControl class the same as number 1.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함