- Home /
The question is answered, right answer was accepted
Null Reference
Hello there, i having problem with this error.
NullReferenceException: Object reference not set to an instance of an object CharacterMovement.Movement () (at Assets/Scripts/CharacterMovement.cs:16) CharacterMovement.Update () (at Assets/Scripts/CharacterMovement.cs:24)
Here is the code...
using UnityEngine; using System.Collections;
public class CharacterMovement : MonoBehaviour
{
// Player Movement Variables/....
public static int movespeed = 1;
void Start()
{
}
public void Movement()
{
if(Switch.GUIValue.text == "Dice Value: 2")
{
transform.Translate(Vector3.right * movespeed * Time.deltaTime);
}
}
public void Update()
{
Movement();
}
}
what am trying to do is, i move my character based upon the dice value. Dice value code is given below..
using UnityEngine; using System.Collections;
public class Switch : MonoBehaviour
{
public static GUIText GUIValue;
public bool showButton = false;
void start()
{
GUIValue.text = "Dice Value: ";
}
public void RunAfterDelay(float delay, System.Action codeToRun)
{
StartCoroutine (DoDelay(0.8f, codeToRun));
}
IEnumerator DoDelay(float delay, System.Action codeToRun)
{
yield return new WaitForSeconds(0.8f);
codeToRun();
}
public void OnGUI()
{
if(showButton)
{
if(GUI.Button(new Rect(10,50,80,30),"Dice Roll"))
{
switch(Random.Range (1, 7))
{
default:
case 1:
animation.Play("One");
RunAfterDelay(animation["One"].length, ()=> GUIValue.text = "Dice Value: 1 ");
transform.position = new Vector3(1.5f, 0.3f, 0.5f);
break;
case 2:
animation.Play("Two");
RunAfterDelay(animation["Two"].length, ()=> GUIValue.text = "Dice Value: 2 ");
break;
case 3:
animation.Play("Three");
RunAfterDelay(animation["Three"].length, ()=> GUIValue.text = "Dice Value: 3 ");
break;
case 4:
animation.Play("Four");
RunAfterDelay(animation["Four"].length, ()=> GUIValue.text = "Dice Value: 4 ");
break;
case 5:
animation.Play("Five");
RunAfterDelay(animation["Five"].length, ()=> GUIValue.text = "Dice Value: 5 ");
break;
case 6:
animation.Play("Six");
RunAfterDelay(animation["Six"].length, ()=> GUIValue.text = "Dice Value: 6 ");
break;
}
}
}
}
public void Update()
{
showButton = true;
}
}
If the dice value is shown by 2 means the character needs to move 2 steps in x-Axis..
-Prasanna
Answer by GameVortex · Feb 19, 2014 at 12:49 PM
Nowhere in your code are you initializing GUIValue, you are only setting GUIValue.text which is not possible before GUIValue has been set. The error does not occur until the Update of CharacterMovement because that is where GUIValue is used first because the Start function in Switch is written wrong and will not be called.
You are also checking for the wrong text in the Movement() function, you set it to: "Dice Value: 2 " and check for "Dice Value: 2". (Notice the space at the end?). I would recommend storing the actual int from the random function and checking against that instead as that is less prone to errors and typos.
Answer by prototype7 · Feb 19, 2014 at 12:50 PM
Maybe you forgot to define your GUIText since its static, so change to public without static and drag and drop your GUIText from inspector
public GUIText GUIValue;
public bool showButton = false;
void start()
{
GUIValue.text = "Dice Value: ";
}