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