Search Unity

Bug Can't fix error

Discussion in 'Scripting' started by Dasero1309, May 31, 2023.

  1. Dasero1309

    Dasero1309

    Joined:
    May 31, 2023
    Posts:
    3
    someone know how to fix this?
    that's my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class BattleH : MonoBehaviour
    7. {
    8.     public Text nameText;
    9.     public Text levelText;
    10.     public Slider hpSlider;
    11.    
    12.     public void SetHUD(Unit unit)
    13.     {
    14.         nameText.text = unit.unitName;
    15.         levelText.text = "Lvl " + unit.unitLevel;
    16.         hpSlider.maxValue = unit.maxHP;
    17.         hpSlider.value = unit.currentHP;
    18.     }
    19.  
    20.     public void SetHP(int hp)
    21.     {
    22.         hpSlider.value = hp;
    23.     }
    24.  
    25. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public enum BattleState { START, PLAYERTURN, ENEMYTURN, WON, LOST }
    8.  
    9. public class BattleSystem : MonoBehaviour
    10. {
    11.  
    12.     public GameObject playerPrefab;
    13.     public GameObject enemyPrefab;
    14.  
    15.     public Transform playerBattleStation;
    16.     public Transform enemyBattleStation;
    17.  
    18.     Unit playerUnit;
    19.     Unit enemyUnit;
    20.  
    21.     public Text dialogueText;
    22.  
    23.     public BattleH playerHUD;
    24.     public BattleH enemyHUD;
    25.  
    26.     public BattleState state;
    27.  
    28.     void Start()
    29.     {
    30.         state = BattleState.START;
    31.         SetupBattle();
    32.     }
    33.  
    34.     void SetupBattle()
    35.     {
    36.         GameObject playerGO = Instantiate(playerPrefab, playerBattleStation);
    37.         playerUnit = playerGO.GetComponent<Unit>();
    38.  
    39.         GameObject enemyGo = Instantiate(enemyPrefab, enemyBattleStation);
    40.         enemyUnit = enemyGo.GetComponent<Unit>();
    41.  
    42.         dialogueText.text = enemyUnit.unitName + " cię zaatakował";
    43.  
    44.         playerHUD.SetHUD(playerUnit);
    45.         enemyHUD.SetHUD(enemyUnit);
    46.     }
    47.  
    48. }
    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)
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    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.
     
    Kurt-Dekker likes this.
  3. Dasero1309

    Dasero1309

    Joined:
    May 31, 2023
    Posts:
    3
    Do you know how to fix this? Because I have no idea and the whole SetHUD function create error
     
  4. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    620
    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.
     
    Kurt-Dekker likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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
     
  6. Dasero1309

    Dasero1309

    Joined:
    May 31, 2023
    Posts:
    3
    Thanks for help