0

I've been looking around for a possible solution but I don't solve It... This is the problem:

I have in a file with its class the various Ui, including the textmesh.

Another file, with its own class, retrieves and sets them via ".text"

result?

"NullReferenceException: Object reference not set to an instance of an object Controller_RaceManager.Update () (at Assets/[GameAssets]/Controller [MobileCar]/Scripts.Code/Controller_RaceManager.cs:307)"

to better understand:

A) into Controller_UiManager.cs (attached to the canvas with all UIs)

using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI; using TMPro;


    public class Controller_UiManager : MonoBehaviour
    {



        [System.Serializable]
        public class UiSettings
        { 


            public TextMeshProUGUI Label_Lap;

        }
        [SerializeField]
        [HideLabel] public UiSettings UiElements;


        void OnDrawGizmos()
        {

            var Canvas  = this.gameObject;

            UiElements.Label_Lap = getTheChild(Canvas, "Label.PilotLap").transform.GetComponent<TextMeshProUGUI>();

            // note: getTheChild is a method that returns the child gameObject

        }


    }

B) Controller_RaceManager.cs, which of course handles all the values โ€‹โ€‹of races (It's attached to his prefab for loops and bla bla bla)

using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using UnityEngine.UI; using TMPro;

public class Controller_RaceManager : MonoBehaviour
{

    [System.Serializable]
    public class RaceManagerSettings
    {

        ...

        public Controller_UiManager UiManager;

    }
    [SerializeField]
    [HideLabel] public RaceManagerSettings sets;


    void OnDrawGizmos()
    {
        if(GameObject.Find("Ui-RaceLoop"))
        {
            sets.UiManager = GameObject.Find("Ui-RaceLoop").transform.GetComponent<Controller_UiManager>();
        }
        else
        {
            Debug.LogError("Controller Race Manage: Ui System not founded - Ui-Raceloop prefab is not in scene");
        }

    }

    void Update()
    {

        string Label_Lap_Output = "";
        if(...)         { Label_Lap_Output = "FINISHED";}
        else if(....)   { Label_Lap_Output = "LAP XX/"+totalloopText; }
        else            { Label_Lap_Output = "LAP O"+pilotlapText+"/"+totalloopText; }

        sets.UiManager.UiElements.Label_Lap.text = Label_Lap_Output;

    }

}

here unity pauses and gives notice. If I take the break, it continues quietly ... but am I becoming stupid?

is there a reason?

note: Unity TextMeshProUGUI.text is NullReference not "why not all or some things" ... but textmeshpro.

Alberto
  • 274
  • 1
  • 5
  • 16
  • 1
    Assuming `sets.UiManager.UiElements.Label_Lap.text = Label_Lap_Output;` is line 307 in **Controller_RaceManager.cs**... One of `sets` or `sets.UiManager` or `sets.UiManager.UiElements` or `sets.UiManager.UiElements.Label_Lap` is null. You haven't shared enough code to tell which one it could be. โ€“ Ruzihm Jun 25 '19 at 22:43

1 Answers1

0

Solved.

I didn't understand why the It was taken from other scripts but, with TextMesh, no.

So it was obvious that I was wondering about him (in part it was right! Because on the buttons it doesn't happen... the "ui controller" is taken via editor OnDrawGizmos function)

The solution:

TextMesh must be initialized in Start or Awake. If you try a taken it directly from the editor with OnDrawGizmo, even if you reset or empty the caches, will be null.

If instead, in start or awake:

void Start()
{
// find ui elements
sets.UiManager = GameObject.Find("Ui-RaceLoop").GetComponent<Controller_UiManager>();
}

The infamous works ... I have no idea why it happens, maybe they are different procedures between the two functions.

Alberto
  • 274
  • 1
  • 5
  • 16