NullReferenceException: Object reference not set to an instance of an object (869496)

Hello there, I am trying to make a scoreboard/highscoreboard but for some reason the only thing that won’t work and the one thing I get an error from is line 39 can someone help me? I only get the error in the title.

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

public class HighscoreTable : MonoBehaviour
{
    private Transform entryContainer;
    private Transform entryTemplate;

    private void Awake()
    {
        entryContainer = transform.Find("highscoreEntryContainer");
        entryTemplate = entryContainer.Find("highscoreEntryTemplate");

        entryTemplate.gameObject.SetActive(false);

        float templateHeight = 30f;

        for (int i = 0; i < 10; i++)
        {
            Transform entryTransform = Instantiate(entryTemplate, entryContainer);
            RectTransform entryRectTransform = entryTransform.GetComponent<RectTransform>();
            entryRectTransform.anchoredPosition = new Vector2(0, -templateHeight * i);
            entryTransform.gameObject.SetActive(true);

            int rank = i + 1;
            string rankString;
            switch (rank)
            {
                default:
                    rankString = rank + "TH"; break;

                case 1: rankString = "1ST"; break;
                case 2: rankString = "2ST"; break;
                case 3: rankString = "3ST"; break;
            }

            entryTransform.Find("posText").GetComponent<Text>().text = rankString;

            int score = Random.Range(0, 120);

            entryTransform.Find("scoreText").GetComponent<Text>().text = score.ToString();

            string name = "AAA";
            entryTransform.Find("nameText").GetComponent<Text>().text = name;
        }
    }

}

Well, either entryTransform or the result of Find or the result of GetComponent are Null.
Split all the functions functions and calls into seperate lines and figure out what fails.

I assume posText is a child of the entryTransform transform? if so you would need to call FindChild or GetComponentInChildren to get the actual component attached.

stanberendsen did you by any chance managed to figure out the problem?I am struggling with the same :slight_smile:

Solving NullReference is three steps and it is ALWAYS THE SAME THREE STEPS… ALWAYS.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Steps to success:

  • Identify what is null
  • Identify why it is null
  • Fix that