Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NullReferenceException Error, Please Help

Discussion in 'Scripting' started by Sobe459, Aug 28, 2012.

  1. Sobe459

    Sobe459

    Joined:
    Aug 28, 2012
    Posts:
    27
    Been working on my GUI Script for a bit expecally the name plate of your target and player. However im getting an error on line 67. Can anyone help me? Much appreciated. Can provide any other info needed. Thank you.

    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. MyGUI2.OnGUI () (at Assets/Scripts/MyGUI2.cs:67)
    3.  

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MyGUI2 : MonoBehaviour {
    5.    
    6.    
    7.     public float buttonWidth = 40;
    8.     public float buttonHeight = 40;
    9.    
    10.    
    11.     private VitalMe vitalScript;
    12.     private EnemyVital enemyVital;
    13.     private bool equipActive = false;
    14.     private int offset = 10;
    15.    
    16.     Rect box = new Rect(20, 30, 160, 20);
    17.     Rect box2 = new Rect(210, 30, 160, 20);
    18.  
    19.     private Texture2D background;
    20.     private Texture2D foreground;
    21.    
    22.     private Transform target;
    23.    
    24.     void Awake() {
    25.         vitalScript = (VitalMe) FindObjectOfType(typeof(VitalMe)); 
    26.     }
    27.    
    28.     void Start() {
    29.        
    30.         background = new Texture2D(1, 1, TextureFormat.RGB24, false);
    31.         foreground = new Texture2D(1, 1, TextureFormat.RGB24, false);
    32.  
    33.         background.SetPixel(0, 0, Color.red);
    34.         foreground.SetPixel(0, 0, Color.green);
    35.  
    36.         background.Apply();
    37.         foreground.Apply();
    38.     }
    39.    
    40.     void Update() {
    41.        
    42.     if(Targetting.Instance.selectedTarget == null){
    43.             return;
    44.         }
    45.         else {
    46.             target = Targetting.Instance.selectedTarget.transform;
    47.             enemyVital = target.GetComponent<EnemyVital>();
    48.         }
    49.     }
    50.    
    51.    
    52.     void OnGUI() {
    53.        
    54.         GUI.Box(new Rect(10, 10, 180, 70), "Player");
    55.        
    56.         GUI.BeginGroup(box);
    57.         {
    58.             GUI.DrawTexture(new Rect(0, 0, box.width, box.height), background, ScaleMode.StretchToFill);
    59.             GUI.DrawTexture(new Rect(0, 0, box.width*vitalScript.curHealth/vitalScript.maxHealth, box.height), foreground, ScaleMode.StretchToFill);
    60.            
    61.         }
    62.         GUI.EndGroup(); ;
    63.        
    64.        
    65.             if(Targetting.Instance.selectedTarget != null){
    66.            
    67.             GUI.Box(new Rect(200, 10, 180, 70), enemyVital.Name);  
    68.            
    69.                 GUI.BeginGroup(box2);
    70.                 {
    71.                     GUI.DrawTexture(new Rect(0, 0, box.width, box.height), background, ScaleMode.StretchToFill);
    72.                 GUI.DrawTexture(new Rect(0, 0, box.width*enemyVital.currentHealth  / enemyVital.maxHealth , box.height), foreground,     ScaleMode.StretchToFill);
    73.                 }  
    74.        
    75.                 GUI.EndGroup(); ;
    76.            
    77.             }
    78.        
    79.         #region equip
    80.         if(equipActive) {
    81.             GUI.Box(new Rect(30, 50, Screen.width / 4, 400), "Equipment  Stats");
    82.             GUI.Label(new Rect(40, 80, 100, 20), "Level: " + vitalScript.curLevel);
    83.             GUI.Label(new Rect(40, 110, 100, 20), "HP: " + vitalScript.curHealth + " / " + vitalScript.maxHealth);
    84.             GUI.Label(new Rect(40, 140, 100, 20), "Mana: " + vitalScript.curMana + " / " + vitalScript.maxMana );
    85.             GUI.Label(new Rect(180, 80, 100, 20), "Endurance: " + vitalScript.endurance);
    86.             GUI.Label(new Rect(180, 100, 100, 25), "Intelligence: " + vitalScript.intellect);
    87.             GUI.Label(new Rect(180, 120, 100, 20), "Perception: " + vitalScript.perception);
    88.             GUI.Label(new Rect(180, 140, 100, 40), "Strength: " + vitalScript.strength);
    89.                
    90.         }
    91.         #endregion
    92.        
    93.  
    94.     }
    95. }
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    enemyVital is null when you try to access it. Since it can be null (you can have nothing targeted) you should check for null before executing that part of the code.
     
  3. Sobe459

    Sobe459

    Joined:
    Aug 28, 2012
    Posts:
    27
    Thank you very much works like a charm, now onto combat scripting:)