- Home /
NullReferenceException problem
I keep getting this NullReferenceException:Object reference not set to an instance of an object Bullet_Hit_Boss.OnTriggerEnter (UnityEngine.Collider.col) (at Assets/Scripts/Bullet_Hit_Boss.cs:16), but on the code below the actual part that is supposidly wrong is the "healthbar.CurHealth -=20;" ....Really cant work out why it wont work! Have practically same scripts in use eleswhere and they work fine! Any help would be appreciated, heres the two scripts being used...
public class Bullet_Hit_Boss : MonoBehaviour {
void OnTriggerEnter(Collider col)
{
if(col.gameObject.tag == "Boss")
{
Debug.Log("Bullet Hit Boss !"); // outputs to console
Boss healthbar = GetComponent<Boss>();
healthbar.CurHealth -= 20;
Debug.Log("Health Decreased !");
Destroy(gameObject);
}
}
}
public class Boss : MonoBehaviour {
public int maxHealth = 100;
public int CurHealth = 100;
public GUISkin HealthBar;
//public GUISkin HealthBarStay;
public float healthBarLength;
//public float healthBarLengthStay;
public bool GUIdisplayed;
// Use this for initialization
void Start ()
{
GUIdisplayed = true;
healthBarLength = Screen.width /3;
// healthBarLengthStay = Screen.width /3;
}
// Update is called once per frame
void Update ()
{
AdjustCurrentHealth(0);
}
void OnGUI()
{
// GUI.skin = HealthBarStay;
//GUI.Box (new Rect(20,400, healthBarLengthStay, 14), curHealth + "/" + maxHealth);
if(GUIdisplayed == true)
{
GUI.skin = HealthBar;
GUI.Box (new Rect(Screen.width/ 10 * 3.5f, Screen.height/ 10 * 6f , healthBarLength, 50),""); // From Right, From Top, Length, Height
}
}
public void AdjustCurrentHealth(int adj)
{
CurHealth += adj;
if(CurHealth <0)
{
CurHealth = 0;
}
if(CurHealth > maxHealth)
CurHealth = maxHealth;
if(maxHealth <1)
maxHealth = 1;
healthBarLength =(Screen.width /3) * (CurHealth / (float)maxHealth);
}
}
Answer by woodoo · Jan 14, 2014 at 11:49 AM
In this line:
Boss healthbar = GetComponent<Boss>();
You are trying to get the component Boss from your class' GameObject. The function GetComponent
returns the component (if found) or null
if not.
Probably it is returning null
, and then the NullReferenceException is thrown when you try to update CurHealth here:
healthbar.CurHealth -= 20;
Check if the GameObject you are trying to GetComponent<>()
really has the component Boss
.
Edit: The GameObject you need to get the component Boss
in this case is the collider's gameObject.
To fix it, change this:
Boss healthbar = GetComponent<Boss>();
to that:
Boss healthbar = col.gameObject.GetComponent<Boss>();
is the part withing the <> in my case Boss, is that not just calling the Boss script and class so that the CurHealth variable can be accessed ? sorry im kind of new to all this! It works fine in another script when i use and the HealthBar1 is only the name of the of the class and it works fine ?
Read the edited slice of my answer above.. You were trying to get the component Boss from your current class, and not from the collided object, which in this case is the Boss itself.
awh man you are a saint! didnt need the whole thing because it was bringing up an error to do with gameobject! it works perfect like this...
Boss healthbar = col.GetComponent();
wont let me put in but thats in too!
Happy to know that it fixed your problem. Don't forget to upvote and check as selected answer if you think the answer was useful.
This answer really helped me out as well. Thank you.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help With AI Error 2 Answers
Scripting error comes up when I press play 1 Answer
Need help calling a script to another keep getting errors 1 Answer
NullReferenceException trouble 1 Answer