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);
}
}