-1

The whole code looks fine to me, but I am a beginner, so please be patient if it is obvious.

My problem is that I get an error

Object reference not set to an instance of object

ehealth is the enemy's health and this is for the assignment of a slime's stats

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

public class EnemyStats : MonoBehaviour
{
    public TMP_Text Text;

    void Update()
    {
        string ehealth = GameObject.Find("VStorage").GetComponent<VStorage>().ehealth.ToString();

        Text.text = ehealth;
    }
}

I don't know what to try so I'm asking for you help. I tried changing some variable names but nothings changed

derHugo
  • 83,094
  • 9
  • 75
  • 115
Mr Glub
  • 19
  • 4
  • 2
    The error doesn't say that the string is empty, it says that one of the object references you are trying to access is`null` - this could be any of `Text` or `GameObject.Find`, etc – UnholySheep Jun 24 '23 at 17:21
  • 3
    You declare `Text` but don't initialize it, so it's a good candidate. Get your debugger, put a breakpoint on thr line before where the exception was thrown (info, btw, you should have included with your question) and inspect all the variables as you step through. You're going to want to get familiar with using the debugger – Flydog57 Jun 24 '23 at 17:26
  • Im not shure how to fix it beacuse the Text is defined and the Vstorage works on other scripts – Mr Glub Jun 24 '23 at 17:27
  • 2
    No, `Text` is declared, but it's not initialized, so it has no value – Flydog57 Jun 24 '23 at 17:29
  • How do i initialise it and how do i get a debugger. I know i might sound like an idiot – Mr Glub Jun 24 '23 at 17:32
  • https://docs.unity3d.com/Manual/ManagedCodeDebugging.html – UnholySheep Jun 24 '23 at 17:36

1 Answers1

0

I think you have to assign your "Text"-variable to a TMP in the inspector:Drag the TextMeshPro (Text) in this slot

Also most c#-programmers name variables with camelCase.