someone know how to fix this? that's my code: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class BattleH : MonoBehaviour { public Text nameText; public Text levelText; public Slider hpSlider; public void SetHUD(Unit unit) { nameText.text = unit.unitName; levelText.text = "Lvl " + unit.unitLevel; hpSlider.maxValue = unit.maxHP; hpSlider.value = unit.currentHP; } public void SetHP(int hp) { hpSlider.value = hp; } } Code (CSharp): 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 BattleH playerHUD; public BattleH enemyHUD; public BattleState state; void Start() { state = BattleState.START; SetupBattle(); } void SetupBattle() { GameObject playerGO = Instantiate(playerPrefab, playerBattleStation); playerUnit = playerGO.GetComponent<Unit>(); GameObject enemyGo = Instantiate(enemyPrefab, enemyBattleStation); enemyUnit = enemyGo.GetComponent<Unit>(); dialogueText.text = enemyUnit.unitName + " cię zaatakował"; playerHUD.SetHUD(playerUnit); enemyHUD.SetHUD(enemyUnit); } } NullReferenceException: Object reference not set to an instance of an object BattleH.SetHUD (Unit unit) (at Assets/Scripts/BattleH.cs:15) BattleSystem.SetupBattle () (at Assets/Scripts/BattleSystem.cs:44) BattleSystem.Start () (at Assets/Scripts/BattleSystem.cs:31)
How to fix a null ref error 1. Find out what is null 2. Find out why it's null 3. Fix it. Your error points to line 15. So either levelText or unit is null.
For some reason "unit" or some part of unit is null. So see where it assigned? Line 37 in BattleSystem.cs. I would confirm that you actually got a value at that point by adding debug statements. There is no way to fix things other than to first determine the cause.
As tley and brath have pointed out, there's only one way for you to solve this problem. I'll post the full pinned link so you can see some discussion links inside to help you along: How to fix a NullReferenceException error https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/ Three steps to success: - Identify what is null <-- any other action taken before this step is WASTED TIME - Identify why it is null - Fix that This is the kind of mindset and thinking process you need to bring to this problem: https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695