Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Pair of fresh eyes please? I cannot figure out what is up with my setup.

Discussion in 'Scripting' started by dreamtocode77, Sep 11, 2020.

  1. dreamtocode77

    dreamtocode77

    Joined:
    Nov 10, 2019
    Posts:
    46
    Project:

    I am literally at the tail end of finishing a Turn Based RPG practice file. I am having a issue i need some new eyes.

    Problem:

    • If the Player character goes first; Attacking a choosing target works, but turn does not switch. I can go forever.(my attck buttons are not suppose to go invisible on enemy turn yet)
    • If Enemy goes first, A.I. attack and target choose works but then, if I hit the attack, I get a null reference exception.
    NRE takes me to 2 different scripts that have no visible errors:

    This script is Line 27.

    Code (CSharp):
    1. public class SelectUnit : MonoBehaviour {
    2.  
    3.     private GameObject currentUnit;
    4.  
    5.     [SerializeField]private GameObject actionsMenu, enemyUnitsMenu;
    6.  
    7.     /*void Awake() {
    8.         SceneManager.sceneLoaded += OnSceneLoaded;
    9.     }*/
    10.  
    11.     private void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
    12.         if (scene.name == "Battle") {
    13.             //this.actionsMenu = GameObject.Find ("ActionsMenu");
    14.             this.enemyUnitsMenu = GameObject.Find ("EnemyUnitsMenu");
    15.         }
    16.     }
    17.  
    18.     public void selectCurrentUnit(GameObject unit) {
    19.         this.currentUnit = unit;
    20.  
    21.         //this.actionsMenu.SetActive (true);
    22.  
    23.         //this.currentUnit.GetComponent<PlayerUnitAction> ().updateHUD ();
    24.     }
    25.  
    26.     public void selectAttack(bool physical) {
    27.         this.currentUnit.GetComponent<PlayerUnitAction>().selectAttack(physical);
    28.         Debug.Log("???");
    29.         //this.actionsMenu.SetActive (false);
    30.         this.enemyUnitsMenu.SetActive (true);
    31.     }
    32.  
    33.     public void attackEnemyTarget(GameObject target) {
    34.         //this.actionsMenu.SetActive (false);
    35.         this.enemyUnitsMenu.SetActive (false);
    36.  
    37.         this.currentUnit.GetComponent<PlayerUnitAction>().act (target);
    38.     }
    39. }

    This script is line 13 and 20

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class AddButtonCallback : MonoBehaviour {
    6.  
    7.     [SerializeField]
    8.     private bool physical;
    9.  
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         this.gameObject.GetComponent<Button> ().onClick.AddListener (() => addCallback());
    14.         Debug.Log("Good");
    15.     }
    16.  
    17.     private void addCallback() {
    18.         GameObject playerParty = GameObject.Find ("PlayerParty");
    19.         Debug.Log("Here");
    20.         playerParty.GetComponent<SelectUnit>().selectAttack (this.physical);
    21.         Debug.Log("true");
    22.     }
    23.  
    24. }
    25.  
    Though the its pointing me to those scripts, I think the NRE is happening because the turns not switching( possible reason why I don't get the NRE when player character has turn first.)

    Code (CSharp):
    1. public class TurnSystem : MonoBehaviour {
    2.  
    3.     [SerializeField]private List<UnitStats> unitsStats;
    4.  
    5.     private GameObject playerParty;
    6.  
    7.     public GameObject enemyEncounter;
    8.  
    9.     [SerializeField]
    10.     private GameObject actionsMenu, enemyUnitsMenu;
    11.  
    12.     void Start() {
    13.         this.playerParty = GameObject.Find ("PlayerParty");
    14.  
    15.         unitsStats = new List<UnitStats> ();
    16.         GameObject[] playerUnits = GameObject.FindGameObjectsWithTag("Red");
    17.         foreach (GameObject playerUnit in playerUnits) {
    18.             UnitStats currentUnitStats = playerUnit.GetComponent<UnitStats> ();
    19.             currentUnitStats.calculateNextActTurn (0);
    20.             unitsStats.Add (currentUnitStats);
    21.         }
    22.         GameObject[] enemyUnits = GameObject.FindGameObjectsWithTag("Blue");
    23.         foreach (GameObject enemyUnit in enemyUnits) {
    24.             UnitStats currentUnitStats = enemyUnit.GetComponent<UnitStats> ();
    25.             currentUnitStats.calculateNextActTurn (0);
    26.             unitsStats.Add (currentUnitStats);
    27.         }
    28.         unitsStats.Sort ();
    29.  
    30.         //this.actionsMenu.SetActive (false);
    31.         this.enemyUnitsMenu.SetActive (false);
    32.  
    33.         this.nextTurn ();
    34.         Debug.Log("Done");
    35.     }
    36.  
    37.     public void nextTurn() {
    38.         GameObject[] remainingEnemyUnits = GameObject.FindGameObjectsWithTag ("Blue");
    39.         if (remainingEnemyUnits.Length == 0) {
    40.             //this.enemyEncounter.GetComponent<CollectReward> ().collectReward ();
    41.             //SceneManager.LoadScene ("Town");
    42.         }
    43.  
    44.         GameObject[] remainingPlayerUnits = GameObject.FindGameObjectsWithTag ("Red");
    45.         if (remainingPlayerUnits.Length == 0) {
    46.             //SceneManager.LoadScene("Title");
    47.         }
    48.  
    49.         UnitStats currentUnitStats = unitsStats [0];
    50.         unitsStats.Remove (currentUnitStats);
    51.  
    52.         if (!currentUnitStats.isDead ()) {
    53.             GameObject currentUnit = currentUnitStats.gameObject;
    54.  
    55.             currentUnitStats.calculateNextActTurn (currentUnitStats.nextActTurn);
    56.             unitsStats.Add (currentUnitStats);
    57.             unitsStats.Sort ();
    58.  
    59.             if (currentUnit.tag == "Red") {
    60.                 this.playerParty.GetComponent<SelectUnit> ().selectCurrentUnit (currentUnit.gameObject);
    61.                 Debug.Log("myturn");
    62.             } else {
    63.                 currentUnit.GetComponent<EnemyUnitAction>().act();
    64.                 Debug.Log("EnemyTurn");
    65.             }
    66.         }
    67.         else
    68.         {
    69.             this.nextTurn ();
    70.             Debug.Log("NEXT");
    71.         }
    72.     }
    73. }
    All debug.logs are just me seeing if code is making it there.
     
  2. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,511
    Can you step through the debugger with breakpoints?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    NRE will not give you a "visible error." It's a logical error and literally the MOST COMMON error ever.

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.
     
    Bunny83 likes this.
  4. dreamtocode77

    dreamtocode77

    Joined:
    Nov 10, 2019
    Posts:
    46
    I am more than aware of this infamous error. What I meant by visible, was the part of script itself that it is taking me too is showing no errors. debug.log it to see if it runs and it does. This situation makes it a little difficult, because I am only getting the error on a certain turn but the other.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    If you click on the error and look in the lower part of the console window, the entire stack of how it blew up at that NRE will be visible, one function call per line, and from that you can isolate lines.

    Also, I consider this:

    Code (csharp):
    1. this.playerParty.GetComponent<SelectUnit> ().selectCurrentUnit (currentUnit.gameObject);
    To be a "hairy line of code," as identified in the post above. If anything in there is null, you can't figure out what is. And that is precisely your current situation.

    Here's what I think of hairy lines of code: http://plbm.com/?p=248
     
  6. dreamtocode77

    dreamtocode77

    Joined:
    Nov 10, 2019
    Posts:
    46
    Code (CSharp):
    1. public void selectAttack(bool physical) {
    2.         if (currentUnit == null)
    3.         {
    4.             Debug.Log("is not working on enemy turn");
    5.         }
    6.         this.currentUnit.GetComponent<PlayerUnitAction>().selectAttack(physical);
    7.         //this.actionsMenu.SetActive (false);
    8.         this.enemyUnitsMenu.SetActive (true);
    9.     }
    I read both of the links last night. A I was able to narrow down the null reference, buuuuuut the code is correct; it's doing what its suppose to do. I believe the issue is from the turnscript.

    It is a turnbased RPG with order depending on speed. When the player's turn is first, I get no nullreference errors(that code above runs). When the enemy is first I get the error, but I believe it is the result of me trying to access player code during a time when its not the players turn, So when its players turn all references are active because its the players turn, but when its the enemy turn reference will "break" until its players turn again. The real issue ( I believe) is regardless of who's turn it is, after attacking the turn does not switch

    sooo because turns are not switching, I press the attack button and that brings up a list of targets, but that window is unactive during enemy turn and because its not switching after attacks, I trying to bring it during a when I have code saying it should not exist at that moment,thus null error.
     
    Last edited: Sep 12, 2020
  7. Pharari

    Pharari

    Joined:
    Feb 18, 2019
    Posts:
    41
    Just to confirm I added a next turn button to manually switch turns between attacks and everything works fine. Turn system is the issue. (same guy but different account because of not auto logged on in phone and remember password :))