- Home /
Object Reference not Set to an Instance of an Object in C# (Closed)
I have a script detecting if an object has collided with another. I've done some tests by calling other variables between the two scripts (now deleted) and that worked fine, no errors. When tiring to set one variable in the script Stats equal to the called variable from the script RocketDamage I get a NullReferenceExeption.
Stats script (error is on the line "isRocketHit = rocketDamage.rocketHit;")
using UnityEngine;
using System.Collections;
public class Stats : MonoBehaviour {
public int playerControl = 1;
public float hp = 1002f;
public float armor = 1005f;
RocketDamage rocketDamage;
public float rocketOut;
float isRocketHit;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//call RocketDamage script because OnTriggerEnter is only allowed once per script
rocketDamage = GetComponent<RocketDamage>();
isRocketHit = rocketDamage.rocketHit;
//take dame in returned that hit is true
if(isRocketHit == 1){
hp -= Mathf.Round(100 + (100/(armor+1)));
armor -= 200;
//output that damage from rocket has been taken
rocketOut = 0;
}
}
//take damage if hit by bullet
void OnTriggerEnter(Collider bullet_basic){
hp -= Mathf.Round(1 + (1000/(armor+1)));
if(armor > 0){
armor -= 5;
}
}
}
RocketDamage script:
using UnityEngine;
using System.Collections;
public class RocketDamage : MonoBehaviour {
Stats stats;
public float rocketHit = 0f;
// Use this for initialization
void Start () {
//call Stats script
stats = GetComponent<Stats>();
}
// Update is called once per frame
void Update () {
//detect if damage from rocket has been taken
if(stats.rocketOut == 0){
rocketHit = 0;
}
}
//detect rocket explosion
void OnTriggerEnter(Collider explosion_basic){
rocketHit = 1;
}
}
Thanks in advance, I been attempting to debug and Google search answers for hours now.
Answer by Slobdell · Jun 16, 2013 at 08:24 PM
What is your RocketDamage component attached to? It's not finding your component because it doesn't know which object to get it from. I'm not sure about your syntax either, may or may not work, but when I get a reference to a script it's a little different. Try changing
RocketDamage rocketDamage = GetComponent<RocketDamage>;
to
RocketDamage rocketDamage = (RocketDamage)this.GetComponent(typeof(RocketDamage));
Or if the script is attached to something else, just change "this" to whatever GameObject the script is attached to. Same thing for getting the stats script.
Steve
The script is part of the same object, yes. I tried changing it to what you had, but it still gives the same error. The error is on the following line.
Did you forget to actually attach the script to the object? Or trying to get it from the wrong object?
I solved this by combining it with the original script, but I had a similar problem with another script. I found my solution with that one. I needed to make a GameObject variavle and set it with GameObject.FindGameObjectWithTag. That solve my problem.
Thanks for your help, Steve.
Your answer
Follow this Question
Related Questions
Re-run Start() and OnGUI() from another script 0 Answers
Multi similar script Components same game object 1 Answer
GetComponentInChildren Returning a Component in Parent 4 Answers
C# about NGUITools.AddChild and GetComponent Null 0 Answers
NullReferenceException on object while earlier in script is does work 0 Answers