Problem with NullReferenceException with drawing text

Hello. I’ve started learning how to code in c# using Unity and I have a problem with C# regarding NullReferenceException. I’ve googled it and tried to find my errors in code, but I’m really starting to going mad here.

The erros is: ```
NullReferenceException: Object reference not set to an instance of an object
FightControls.FixedUpdate () (at Assets/Scripts/FightControls.cs:45)


And my script looks lik this: 

```csharp
public class FightControls : MonoBehaviour {

    public enum BattleStates {
        START,
        PLAYERTURN,
        ENEMYTURN,
        END
    }

    private BattleStates currentState;
    public Text Yuzu_HP;
    public Text Yuzu_MP;
    public Text Enemy_HP;
    public Text Enemy_MP;
    //public Text Winner_Text;
    public Text Yuzu_Name;
    public Text Enemy_Name;
   

    // Use this for initialization
    void Start() {
        currentState = BattleStates.START;
    }
   
  
    public void FixedUpdate()
    {

        int Yuzu_HP_Value = GameObject.Find("Yuzu").GetComponent<YuzuStats>().HP;
        int Yuzu_MP_Value = GameObject.Find("Yuzu").GetComponent<YuzuStats>().MP;
        Yuzu_HP.text = "HP: " + Yuzu_HP_Value;
        Yuzu_MP.text = Yuzu_MP_Value + " :MP";
       

        int Enemy_HP_Value = GameObject.Find("Enemy").GetComponent<EnemyStats>().HP;
        int Enemy_MP_Value = GameObject.Find("Enemy").GetComponent<EnemyStats>().MP;
        Enemy_HP.text = "HP: " + Enemy_HP_Value;
        Enemy_MP.text = Enemy_MP_Value + " :MP";

        Enemy_Name.text = "Punk";
        Yuzu_Name.text = "Yuzu";
...
}

The problem is with Yuzu and Enemy Names. The HP and MP works just fine and I can’t realy understand why it’s not finding the Yuzu_Name and Enemy_Name components. They do exist in the game, it looks like this:

I can’t really understand why the Yuzu_MP, HP and Enemy_HP, MP works, but not the names. I’ve got the same problem with Winner_Name - it’s code looks like this:

public void WinnerText()
    {
       
        int Yuzu_HP_Value = GameObject.Find("Yuzu").GetComponent<YuzuStats>().HP;
        int Enemy_HP_Value = GameObject.Find("Enemy").GetComponent<EnemyStats>().HP;
        if (Enemy_HP_Value <= 0)
        {
            Winner_Text.text = "Yuzu Wins";
        }
        if (Yuzu_HP_Value <= 0)
        {
            Winner_Text.text = "Punk Wins";
        }
        else
        {
            currentState = BattleStates.PLAYERTURN;
        }
    }

Sorry for being a dumbass and not being able to see propably a really easy solution, but I’m going mad here.

Find() isn’t a wildcard search, you need the exact name. You really shouldn’t be using it at all – and especially not in FixedUpdate – better to just link the references in the inspector.

1 Like

I’m new to unity’s C#, so I’m just using a method that proved to be working. What better options would you suggest? Anyways, any ideas how should I print out names of my characters, as I’m trying to do in lines 40. and 41.?

Create the linked references in the inspector, as suggested. Also, update the text when the values change.
If it’s names, set the text when the names are chosen (and/or changed).

Similarly, for HP/MP and winners… change the text when the values change / a player wins, etc… :slight_smile: