0

I'm kinda new in C#. I used Unity and C# for the project. I got a problem while I tried to change a text labels content with pressing a button. I tried everything, I watched all the tutorials but still got that massage: " Object reference not set to an instance of an object". I attach some parts of the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.IO;
...
public class LaboratoryController : MonoBehaviour
{
    public Text labelText;
    public void ChangeText()
    {
        labelText.text = "Example";
    }
}
  • Are you sure `labelText` is referenced vis the Inspector? – derHugo Oct 03 '21 at 09:02
  • Yes, it is referenced. – Márton Kakukk Oct 03 '21 at 09:10
  • If it were there would be no such error at least from the code you provided .. are a) sure there is only one instance of this script and that there it actually is referenced the moment the method is called .. b) are you sure that this is the script/line throwing the exception? – derHugo Oct 03 '21 at 09:12
  • It' s only one instance, and it is referenced the moment the method is called. I'm pretty sure that this line that is throwing the exception. Also i did everything like it was in the tutorials. If you want I can post the whole script – Márton Kakukk Oct 03 '21 at 09:17
  • c# doesn't just invent errors ;) so in the moment it is trying to access that field it is not assigned .. please try [Debugging your code](https://docs.unity3d.com/Manual/ManagedCodeDebugging.html) – derHugo Oct 03 '21 at 09:19
  • I used the Debugger in Unity, and I got 2 errors and 6 warnings. the errors are as follows: NullReferenceException: Object reference not set to an instance of an object The referenced script (Unknown) on this Behaviour is missing! – Márton Kakukk Oct 03 '21 at 09:33

0 Answers0