1

On the project that I am working, I have a screen that has a list of "star system" that will eventuyally be clickable. The system should be spawning as a list with names, details and an image. If I force the amount of systems to spawn, They do so correctly, however, once I add the 'for' loop to tie the details in form the inspector, I am getting the following error. Also note that even though I have 4 items added on to the list, only two spawn and none of the input details are shown on the images. I have a feeling that the TextMeshPro system is causing the issue, as the example i am working off of from a tutorial is using an older version of unity and not TMP. Also note that the Destroy(mapSelectorTemplate); is not destroying the GameObject inside the Unity Editor as it should be.

NullReferenceException: Object reference not set to an instance of an object LocationManager.Start () (at Assets/Scripts/LocationManager.cs:37)

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

public class LocationManager : MonoBehaviour
{
   [Serializable]
    public struct Locations
    {
        public int LocationID;
        public int SystemID;
        
        public string LocationName;
        public string SystemName;
        public string DockType;
        public string IndustryType;

        public Sprite LocationImage;

        public float DistanceFromStar; 
    } 

    [SerializeField] Locations[] allLocations; 

    void Start()
    {
        GameObject MapSelectorTemplate = transform.GetChild (0).gameObject;
        GameObject g;
        int N = allLocations.Length;
        for (int i = 0; i < N; i++)
        {
            g = Instantiate (MapSelectorTemplate, transform);
            g.transform.GetChild (0).GetComponent <Image> ().sprite = allLocations [i].LocationImage;
            g.transform.GetChild (1).GetComponent <Text> ().text = allLocations [i].SystemName;
            g.transform.GetChild (2).GetComponent <Text> ().text = allLocations [i].LocationName;
            g.transform.GetChild (3).GetComponent <Text> ().text = allLocations [i].DockType;
            g.transform.GetChild (4).GetComponent <Text> ().text = allLocations [i].IndustryType;
            g.transform.GetChild (5).GetComponent <Text> ().text = allLocations [i].SystemName;  

            
        }
        Destroy (MapSelectorTemplate);
    }


}```


using UnityEngine;
using UnityEngine.UI;
using System;
using TMPro;



public class Demo : MonoBehaviour


{
    TextMeshProUGUI textmeshPro;
    [Serializable]
    public struct Game
    {
        public TMP_Text gameName;
        public TMP_Text gameDescription;
        public Sprite gameIcon;
    }

    [SerializeField] Game[] allGames;

    void Start ()
    {
        textmeshPro.text = GetComponent<TextMeshProUGUI>();
        GameObject buttonTemplate = transform.GetChild (0).gameObject;
        GameObject g;

   

        int N = allGames.Length;

        for (int i = 0; i < N; i++) 
        {
            g = Instantiate (buttonTemplate, transform);
            g.transform.GetChild (0).GetComponent <Image> ().sprite = allGames [i].gameIcon;
            g.transform.GetChild (1).GetComponent <TextMeshProUGUI> ().text = allGames [i].gameName;
            g.transform.GetChild (2).GetComponent <TextMeshProUGUI> ().text = allGames [i].gameDescription;

    
        }

        Destroy (buttonTemplate);
    }


}
Lexicon11
  • 11
  • 2
  • Unfortunately it doesn't (but it did shed some light on the error as a whole so thanks for that). I've seen a version of this code work so I was hoping it was something obvious that I wasn't seeing. – Lexicon11 Feb 10 '23 at 00:32
  • These kinds of exceptions can be hard to track down since they seem to lack detail, but in this case you get a helpful line number - 37. That's around when you start calling `GetChild()` and `GetComponent()`. Try throwing in some Debug.Log statements around there to figure out what might be null in your code. Is it the child? Is it the component? Or is it the item in `allLocations`? – Serlite Feb 10 '23 at 01:16
  • For TextMeshPro you should use GetComponent.text – Cadmonkey33 Feb 10 '23 at 11:00
  • So I removed the textmeshpro and used legacy text and it works as expected. I tried switching the code to and still got the object reference error. I'm looking into why it's not playing well with TMP, would rather use that than legacy text. – Lexicon11 Feb 10 '23 at 15:19
  • @Lexicon11 It's not clear from your code, but if you are using canvas elements, you should use GetComponent.text. TextMeshProUGUI is the Canvas version of TextMeshPro. – Cadmonkey33 Feb 11 '23 at 03:59
  • @Cadmonkey33 Yes, everything is on a Canvas (I do not see an option for a non TMP Canvas so I am using the only option available). I just tried your suggestion and am now getting the error CS0029: Cannot implicitly convert type 'TMPro.TMP_Text' to 'string'. I've also edited my initial post to show the new code i'm testing with. – Lexicon11 Feb 11 '23 at 05:05
  • @Lexicon11 .text expects a string. You are passing it a TMP_Text object. I'm not sure why you're using a TMP_Text objects for gameName and gameDescription, but it will probably work if you pass gameName.text and gameDescription.text. ie. GetCompenent().text = allGames[i].gameName.text; – Cadmonkey33 Feb 11 '23 at 05:26
  • @Cadmonkey33 I was trying to pass them as TMP_Text thinking that would fix the object reference error. I added .text at the end of lines 37 and 38 as you suggested, as well as switched gameName and gameDescription back to string 'public string gameName;' and now i'm getting the error CS1061: 'string' does not contain a definition for 'text' and no accessible extension method 'text' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) – Lexicon11 Feb 11 '23 at 19:59
  • Figured it out. There was a line that was causing a conflict. Just having TextMeshProUGUI next to the get component did the trick. No need for the .text at the end. Thanks for your help everyone, especially you @Cadmonkey33 – Lexicon11 Feb 11 '23 at 23:00

0 Answers0