I'm getting this error message whenever I press play:
NullReferenceException: Object reference not set to an instance of an object BattleSystem+<SetupBattle>d__14.MoveNext () (at Assets/BattleSystem.cs:54) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <2a3b1fc140ae44e9a4476e34a2a29726>:0) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) BattleSystem:Start() (at Assets/BattleSystem.cs:36)
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public enum BattleState { START, PLAYERTURN, ENEMYTURN, WON, LOST }
public class BattleSystem : MonoBehaviour
{
public GameObject playerPrefab;
public GameObject enemyPrefab;
public Transform playerBattleStation;
public Transform enemyBattleStation;
Unit PlayerUnit;
Unit EnemyUnit;
public Text dialogueText;
public Button Attack;
public Button Heal;
public Button Block;
public BattleHUD PlayerHUD;
public BattleHUD EnemyHUD;
public BattleState state;
// Start is called before the first frame update
void Start()
{
state = BattleState.START;
StartCoroutine(SetupBattle());
}
IEnumerator SetupBattle()
{
//instantiate player and enemy GameObjects
GameObject playerGO = Instantiate(playerPrefab, playerBattleStation);
PlayerUnit = playerGO.GetComponent(typeof(Unit)) as Unit;
GameObject enemyGO = Instantiate(enemyPrefab, enemyBattleStation);
EnemyUnit = enemyGO.GetComponent(typeof(Unit)) as Unit;
if (EnemyUnit == null)
{
Debug.LogError("No EnemyUnit component found");
}
PlayerHUD.SetHUD(PlayerUnit);
EnemyHUD.SetHUD(EnemyUnit);
I tried to change the code slightly to see if it would work any other way. I'm new to unity and c# so I'm not 100% sure how to fix this. Please help me.