Search Unity

[C#] I'm getting an error message, but my code is working?

Discussion in 'Scripting' started by SwaggyMcChicken, Jun 28, 2015.

  1. SwaggyMcChicken

    SwaggyMcChicken

    Joined:
    Apr 13, 2015
    Posts:
    108
    I'm trying to practice my coding skills by remaking some mechanics I like from games, currently I'm trying to remake the battle mechanic from the original Pokemon titles. I have a UI system made of multiple canvas's, depending on what level of interaction the user is in, (I'm not sure if this is efficient) and it consists of four buttons. The first set of buttons work just fine, when I click the "Attack button" it switches the attack menu where it lists the four attacks the Pokemon has, these moves are listed by a script called "UINav" which deals with anything related to the UI and the names of the moves are taken from another script named "BattleSystem" which deals with anything related to battles so it stores the four moves of the Pokemon as a variable. So I pull these variables from BattleSystem and replace the text of the button's with this text. This works perfectly, yet I get an error on line 26 of UINav which says "Object reference not set to an instance of an object." Here's the code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class UINav : MonoBehaviour {
    6.    
    7.     //Declaring variable for the BattleScript script
    8.     public BattleScript BS;
    9.    
    10.     //Declaring variables for the UI
    11.     public Text Move1Text;
    12.     public Text Move2Text;
    13.     public Text Move3Text;
    14.     public Text Move4Text;
    15.     public Canvas BattleUI;
    16.     public Canvas AttackUI;
    17.    
    18.     void Start () {
    19.        
    20.         //Getting the component for BS
    21.         BS = GetComponent<BattleScript>();
    22.        
    23.         //Ensuring the BattleUI will be the only one seen
    24.         BattleUI.enabled = true;
    25.         AttackUI.enabled = false;
    26.    
    27.     }
    28.    
    29.     void Update () {
    30.        
    31.         //Setting the Move button's to the respective variables
    32.         Move1Text.text = BS.Move1;
    33.         Move2Text.text = BS.Move2;
    34.         Move3Text.text = BS.Move3;
    35.         Move4Text.text = BS.Move4;
    36.    
    37.     }
    38.    
    39.     //When the Attack button is pressed function
    40.     public void AttackPressed () {
    41.        
    42.         BattleUI.enabled = false;
    43.         AttackUI.enabled = true;
    44.    
    45.     }
    46.    
    47. }
    I've always had an issue when trying to reference outside variables and I've never understood it too well since it wasn't covered much in my basic programming class, so when dealing with this it tends to confuse me. Can someone perhaps shed some light on this subject? I've tried to look up solutions on my own but none seem to cater to my issue.
     
  2. Pharan

    Pharan

    Joined:
    Oct 28, 2013
    Posts:
    102
    Line 26 is empty so the error doesn't make sense. Something weird is going on.

    Are you using MonoDevelop? Did you normalize the line endings for your file?
     
    SwaggyMcChicken likes this.
  3. SwaggyMcChicken

    SwaggyMcChicken

    Joined:
    Apr 13, 2015
    Posts:
    108
    Sorry, I messed up. I commented all my code to help organize it and it moved up the code, ahh I didn't think of that!
    The error is now on line 32.
     
  4. Pharan

    Pharan

    Joined:
    Oct 28, 2013
    Posts:
    102
    Well, did you assign values to Move1Text... etc in the inspector?

    My other suspicion would be that you have one that's set up correctly, and you accidentally added your UINav component to another GameObject and that's what's throwing errors.
     
  5. SwaggyMcChicken

    SwaggyMcChicken

    Joined:
    Apr 13, 2015
    Posts:
    108
    Yeah I setup Text in the inspector. Like I said, everything's working just fine, I'm just being thrown an error message even though it's acting like there isn't one.
     
  6. Pharan

    Pharan

    Joined:
    Oct 28, 2013
    Posts:
    102
    100% sure that there isn't another gameobject or duplicate that you accidentally generated while editing?

    If you click on the error in the console window, it should highlight the GameObject the error came from in the Hierarchy panel/view.
     
  7. SwaggyMcChicken

    SwaggyMcChicken

    Joined:
    Apr 13, 2015
    Posts:
    108
    Ooh okay, I see now. I had it on my button, thanks man.
    I didn't think that would be an issue, you learn something everyday.