0

I have a tank, that has level. Tank is a prefab. Tank should show his level using TextMeshPro:

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

public class TankEnd : MonoBehaviour
{
    GroundSpawner groundSpawner;

    public TextMeshProUGUI levelText;

    public int level; 

    void Start()
    {
        levelText = GameObject.Find("TankEndLevelText").GetComponent<TextMeshProUGUI>();
        groundSpawner = GameObject.Find("GroundSpawner").GetComponent<GroundSpawner>();
        
        level = groundSpawner.level;
        levelText.text = "Level: " + level;
    }

}

But it doesn't changes. I have an error:

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

How to fix this? I think tank script can't find level text.

  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – BugFinder May 07 '22 at 23:56

1 Answers1

0

Do you need the GameObject.Find method? I'd suggest assigning it in the editor if possible or just double check the name, in case it changed somehow

Winjas Force
  • 56
  • 1
  • 2