I cant seem to get this piece of code to work I get a ScalevalueDisplay.Start () (at Assets/ScalevalueDisplay.cs:14)NullReferenceException: Object reference not set to an instance of an object issue.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScalevalueDisplay : MonoBehaviour {
Text text;
public const int ScaleValue = 3;
// Use this for initialization
void Start ()
{
text.text = "" + ScalevalueDisplay.ScaleValue;
}
// Update is called once per frame
void Update () {
}
}
You need to change the “Text text” to be “public Text text” and then assign a Text object to it in the inspector (drag text object in hierarchy onto the public text field in the inspector.
It’s just for debugging. If text won’t display then you didn’t actually set the Text object from inspector correctly.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScalevalueDisplay : MonoBehaviour {
public Text text;
public const int ScaleValue = 3;
void Start () {
if (text) text.text = ScaleValue.ToString();
}
void Update () {
}
}
thanks that seems to solve it however trying to expand the script again so I can have something that updates another text box has seemed to have broken it and told me that the left hand side if an assignment must be a variable, a property or indexer at this line:
ScalevalueDisplay.CurrentcounterTotal += d.Countervalue;
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class OdddropZone : MonoBehaviour , IDropHandler , IPointerEnterHandler, IPointerExitHandler
{
public DragableObject.OddEven OEstate = DragableObject.OddEven.ODD;
public void OnPointerEnter(PointerEventData EventData)
{
//Debug.Log("OnPointerEnter");
}
public void OnPointerExit(PointerEventData EventData)
{
//Debug.Log("OnPointerExit");
}
public void OnDrop(PointerEventData EventData)
{
Debug.Log("OnDrop to " + gameObject.name);
DragableObject d = EventData.pointerDrag.GetComponent<DragableObject>();
if (d != null)
{
if (OEstate == d.OEstate) // if counter is oddd then
{
if (ScalevalueDisplay.CurrentcounterTotal + d.Countervalue <= ScalevalueDisplay.ScaleValue) //if all counters value in play is less then the scales score then
{
d.originalparent = this.transform; //put counter into zone
ScalevalueDisplay.CurrentcounterTotal += d.Countervalue; // add the counter value to the total counters in play
if (text) text.text2 = ScaleValue.ToString(); // change the current display value
}
}
}
}
}
current code for the display:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScalevalueDisplay : MonoBehaviour {
public Text text;
public Text text2;
public const int ScaleValue = 3;
public const int CurrentcounterTotal = 0;
// Use this for initialization
void Start ()
{
//text.text = "" + ScalevalueDisplay.ScaleValue;
if (text) text.text = ScaleValue.ToString();
}
// Update is called once per frame
void Update () {
}
}
sorry, I thought const meant that it would only reference a single instance of CurrentCounterTotal -
if its public static int CurrentcounterTotal will that enable the variable to be changed