1

Unity is saying that something is going wrong in the void update()

any ideas as to what this may be?

I have all the variables defined below but Unity says that some object reference is not set to an instance of an object

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameStatus : MonoBehaviour
{
    public Text PointsCounter;

    public int FightPoints = 0;
    public int FightDamage = 1;


    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        PointsCounter.text = "Points: " + FightPoints;
    }

    private void Awake()
    {
        DontDestroyOnLoad(gameObject);

    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • UnityScript is a Javascript derivative created for Unity. C# is a completely different language. Please decide which language you are using (hint: it's C#, not UnityScript). – ProgrammingLlama Jan 09 '20 at 01:27
  • 1
    @John_ReinstateMonica in addition: UnityScript is deprecated. C# is now the only language supported by Unity (except native plugins ofcourse) – derHugo Jan 09 '20 at 06:49

1 Answers1

1

You never create an instance of PointsCounter so it is null when you hit the line PointsCounter.text = "Points: " + FightPoints;

Instantiate the PointsCounter:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameStatus : MonoBehaviour
{
    public Text PointsCounter;

    public int FightPoints = 0;
    public int FightDamage = 1;


    // Start is called before the first frame update
    void Start()
    {
        PointsCounter = new Text(); // may or may not work; depends on if UnityEngine.UI.Text has an empty constructor; it probably doesn't, see explanation below if not
    }

    // Update is called once per frame
    void Update()
    {
        PointsCounter.text = "Points: " + FightPoints;
    }

    private void Awake()
    {
        DontDestroyOnLoad(gameObject);

    }
}

Though, given that you are using Unity, PointsCounter probably needs to be assigned using the editor. Go to your Unity Hierarchy and ensure the GameObject with this GameStatus behavior has a PointsCounter property set in the UI

gabriel.hayes
  • 2,267
  • 12
  • 15
  • 3
    It's a bad practice to create a new instance of type `UnityEngine.UI.Text` using the `new` operator. – Hamid Yusifli Jan 08 '20 at 20:02
  • 1
    @0xBFE1A8 Thank you; I wasn't completely certain about the internal workings of `UnityEngine.UI` but I had a strong feeling it wasn't something to be instantiated via code. – gabriel.hayes Jan 08 '20 at 20:07
  • Though, given that you are using Unity, PointsCounter probably needs to be assigned using the editor. Go to your Unity Hierarchy and ensure the GameObject with this GameStatus behavior has a PointsCounter property set in the UI – Richard Batsbak Jan 08 '20 at 20:12
  • could you clarify on this? – Richard Batsbak Jan 08 '20 at 20:12
  • @RichardBatsbak In Unity, when you declare public members on a `MonoBehavior` and add that Behavior/Component to a `GameObject` in the hierarchy, all of the public members are editable in the interface. If you select the GameObject that you've given this behavior to in the hierarchy, you should be able to see the members here (PointsCounter, FightPoints, FightDamage) as form controls you can use to change the value. For example, if you had a 'Fighter' with a public property "Health" you could set the Health value to whatever you wanted in via the editor for the GameObejct in the hierarchy. – gabriel.hayes Jan 08 '20 at 20:15
  • 1
    @0xBFE1A8 not only "bad practice" but rather an absolute no-go! The `new` keyword is forbidden for most of the Unity built-in class types with a few exceptions ;) – derHugo Jan 09 '20 at 06:51