I am having a problem and i can’t figure it out. I am still learning how to program in C# and i was hoping someone can help me.
What i am doing is making a “framework” for the GUI/HUD in a FPS game. The problem is that i have an NulReferenceException.
When the error occurs, the console write me this: “NullReferenceException: Object reference not set to an instance of an object GUI_HUD.OnGUI() (at Assets/Scripts/GUI_HUD.cs:53)”
Can you guys help me?
using UnityEngine;
using System.Collections;
public class GUI_CustomControls : MonoBehaviour
{
//Left player HUD side
public void LeftStatusMeter(float health, Texture playerImage, Texture backgroundBarImage, Texture healthBarImage)
{
GUI.BeginGroup(new Rect(0, 0, 330, 125));
//Back bars
GUI.Label(new Rect(40, 10, 272, 90), backgroundBarImage);
//Front bars
GUI.BeginGroup(new Rect(40, 10, 218 * (health / 10.0f) + 35, 90));
GUI.Label(new Rect(0, 0, 272, 90), healthBarImage);
GUI.EndGroup();
//Place head circle
GUI.Label(new Rect(0, 0, 330, 125), playerImage);
GUI.EndGroup();
}
}
using UnityEngine;
using System.Collections;
public class GUI_HUD : MonoBehaviour
{
public GUISkin customSkin;
//Top right player HUD side
public Texture2D playerImage; //Done
public Texture2D playerHealthBarImage; //Done
public Texture2D playerHealthBar; //Done
//-----------------
private GUI_CustomControls customControls; //Done
//private GameObject customControls; //Done
Player_Status playerInfo; //Done
private Player_AttackController playerAttack; //Done
private Component closestEnemyStatus; //Done
//private GameObject closestEnemyStatus; //Done
private GameObject player; //Done
GameObject closestEnemy;
Vector3 enemyDistance;
// Use this for initialization
void Awake ()
{
//customControls = GetComponent<GUI_CustomControls>(); //Done
GUI_CustomControls customControls = (GUI_CustomControls)this.GetComponent(typeof(GUI_CustomControls));
//customControls = FindObjectOfType(typeof(GUI_CustomControls));
playerAttack = GetComponent<Player_AttackController>(); //Done
playerInfo = GetComponent<Player_Status>(); //Done
player = GameObject.FindWithTag(Tag_TagController.player); //Done
}
void OnGUI()
{
if(customSkin)
{
GUI.skin = customSkin;
}
//Players's Vitals
customControls.LeftStatusMeter(playerInfo.maxHealth, playerImage, playerHealthBarImage, playerHealthBar);
}
}
In console click on error to see where exactly is it generated in your code and post only the relevant part of code using “CODE” tag
EDIT:
you do:
customControls.LeftStatusMeter
but variable customControls although is defined to contain reference to object of type GUI_CustomControls it is never assigned any value. So yes it has value null and referencing a null object will generate NullReferenceException
This code declares new variable with name customControls that will exist only within this function and assigns a value to it. That customControls variable is a diferent variable then
public class GUI_HUD : MonoBehaviour
{
private GUI_CustomControls customControls;
In your GUI_HUD script you are declaring a global value customControls in line 15 but then you trying to instantiate the GUI_CustomControls class to a local customControls value in line 31. As its local its only valid in its scope (Awake) and will never reach the OnGUI event
Change line 31 to: customControls = (GUI_CustomControls)this.GetComponent(typeof(GUI_CustomControls));
Or instead of adding your GUI_CustomControls component programmatically you could add it in editor in which case you need to protect yourself from null referencing:
Thank you everyone for your help! I appreciated it very much.
I got it working now, and for everyone else having the same problem i have posted the working code down below:
using UnityEngine;
using System.Collections;
[AddComponentMenu("GUI/HUD")]
public class GUI_HUD : MonoBehaviour
{
public GUISkin customSkin;
//Top right player HUD side
public Texture2D playerImage;
public Texture2D playerBGHealthBar;
public Texture2D playerHealthBar;
//-----------------
private GUI_CustomControls customControls;
private Player_Status playerInfo;
// Use this for initialization
public void Awake ()
{
//this.gameObject.AddComponent("GUI_CustomControls");
this.gameObject.AddComponent("Player_Status");
customControls = (GUI_CustomControls)this.GetComponent(typeof(GUI_CustomControls));
playerInfo = (Player_Status)this.GetComponent(typeof(Player_Status));
}
public void OnGUI()
{
if(customSkin)
{
GUI.skin = customSkin;
}
//Players's Vitals
if (customControls != null)
customControls.LeftStatusMeter(playerInfo.maxHealth, playerImage, playerBGHealthBar, playerHealthBar);
}
}
using UnityEngine;
using System.Collections;
[AddComponentMenu("GUI/Custom Controls")]
public class GUI_CustomControls : MonoBehaviour
{
//Left player HUD side
public void LeftStatusMeter(float health, Texture playerImage, Texture backgroundBarImage, Texture healthBarImage)
{
GUI.BeginGroup(new Rect(0, 0, 330, 125));
//Back bars
GUI.Label(new Rect(40, 10, 272, 90), backgroundBarImage);
//Front bars
GUI.BeginGroup(new Rect(40, 10, 218 * (health / 10.0f) + 35, 90));
GUI.Label(new Rect(0, 0, 272, 90), healthBarImage);
GUI.EndGroup();
//Place head circle
GUI.Label(new Rect(0, 0, 256, 128), playerImage);
GUI.EndGroup();
}
}