text box issue with a number

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.

I have done as you said and set the Text to public and dragged it from the hierarchy to the Text field however I still have the error

I’d try what this does

if (text) text.text = ScaleValue.ToString();

I mean, it’s const yeah but not static unless defined? So i wouldn’t use class name there.

sorry I’m not sure what you mean by “put this there”, do you mean to replace text.text= “” + ScalevalueDisplay.ScaleValue; .

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 () {
   
    }
}

public const int CurrentcounterTotal = 0;

ScalevalueDisplay.CurrentcounterTotal+= d.Countervalue;

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