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.