- Home /
Problem with accessing variable form other scripts.
I'm trying to make a button that makes a variable in another script true but i get this error every time. Please help. Code:
Error: NullReferenceException: Object reference not set to an instance of an object Gui.OnGUI () (at Assets/Scripts/Gui.js:29)
#pragma strict
var moveButtonHight : int;
var moveButtonWidth : int;
var attackButtonHight : int;
var attackButtonWidth : int;
var tempName : String = "hi";
public var CreateCharacter : boolean = false;
function Start () {
}
function Update () {
}
function OnGUI() {
if (GUI.Button(Rect(Screen.width / 2, Screen.height - moveButtonHight, moveButtonWidth, moveButtonHight), "Move")){
Debug.Log("Clicked move");
var player = GameObject.Find("player").GetComponent(player).Moving;
//var com = player.GetComponent(player);
player = true;
}
if (GUI.Button(Rect(Screen.width / 2 - attackButtonWidth, Screen.height - moveButtonHight, attackButtonWidth, attackButtonHight), "Attack / Defend")){
Debug.Log("Clicked Attack / Defend");
}
if (Input.GetKeyUp(KeyCode.C)){
Debug.Log("creating character");
tempName = GUI.TextArea (Rect (Screen.width /4 , Screen.height / 4, 200, 100), tempName, 16);
}
}
Does it throw a null without the var com... line? If so looks like the Component "player" is null, or maybe it just need to be capital "Player"
Try getting the var every frame, put GetComponent in the update function, maybe that helps.
Try rewriting it so that you don't have to use GameObject.Find for anything. You should never have to use that function.
Just a random thought, maybe call your javascript something else other than Gui, since that might be confusing the script compiler. Also, what is line 29 in your code?
Answer by Piflik · May 05, 2012 at 05:37 PM
This is not the problem throwing the error message, but the 'var player...' line is probably not doing what you think it is doing. You assign the new player variable the value of the Moving variable in your player script (probably False), not the variable itself. Changing player to True will not affect the Moving variable in the player script.
Regarding the Error: be sure to pay attention to case sensitivity. Player != player. Also try GameObject.FindWithTag...I think I remember having problems with GameObject.Find...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Accessing A Variable From Another Script 4 Answers
Accessing another var on another script 1 Answer
accessing a variable from another script. 3 Answers
is this correct way to change var from other script? 1 Answer