0

You don't have to read all the code, my question is a bit further down. Thank you for helping :s

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

public class mouseball : MonoBehaviour
{
    public float speed;
    private int count;
    public Text countText;
    // Use this for initialization
    void Start()
    {
        count = 0;
        setcounttext();

    }

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0, moveVertical);

        GetComponent<Rigidbody>().AddForce(movement * speed * Time.deltaTime);
    }

    void OnTriggerEnter(Collider other)
    {
        Debug.Log("trigger");
        if (other.gameObject.tag == "pickup")
        {
            other.gameObject.SetActive(false);
            count = count + 1;
            setcounttext();
        }
    }

    void setcounttext()
    {
        countText.text = "Count : " + count.ToString();
    }

}

The lines that the error is referencing to are:

void setcounttext()
{
    countText.text = "Count : " + count.ToString();
}

I tried but couldn't find the solution which is prob are easy one, I'm grateful for any help.

DiplomacyNotWar
  • 31,605
  • 6
  • 57
  • 75
  • So lets take a back, do you know what a reference type is? do you know what a null reference exception is? if not, you are likely not at the stage of asking a question here. There are 1000s of duplicates, and a wealth of information available to teach yourself – TheGeneral Mar 24 '21 at 07:52
  • `countText` is null. I don't see anywhere in your code that it's being initialised. Are you initialising it somewhere else? – DiplomacyNotWar Mar 24 '21 at 07:53
  • @Llama i haven't because i'm new to unity and c#. – Abdul Wahab Mar 24 '21 at 07:56
  • @Llama on the other hand this is Unity and it might be initialized via the Inspector ;) But yeah in this case it seems it is actually not referenced at all – derHugo Mar 24 '21 at 07:59

0 Answers0