- Home /
Object reference not set to an instance of an object... again
So first what is the goal of my code? Helicopter 1 fires a rocket on helicopter 2. In flight the rocket shoots a ray onto the target helicopter. Now i want helicopter 2 to recognize when a rocket is flying towards him so i wrote this:
if(Physics.Raycast(transform.position, fwd, out hit))
{
hit.collider.gameObject.GetComponent<damageHandler().RocketAlert();
}
the method RocketAlert:
public void RocketAlert()
{
Debug.Log("ROCKET ALERT!!!");
Instantiate(flare, flareLauncher.transform.position, Quaternion.identity);
}
So that was what i wanted to do and what did i get? The good old "Object reference not set to an instance of an object" error. What i already checked: Helicopter 2 (whos being shot at) got the damageHandler script attached.
Iam close to killing myself i worked on this little F** error for 3 or 4 hours now and i dont now what to do anymore... HELP ME!
btw sorry for bad english iam from germany...
greetz Jannick
how about including as much of the stack trace as possible and the actual lines in script it's referring to, looking at what you posted, it could be the flare variable, flareLauncher, etc etc. Please add more specificity to your question. Null exception questions happens here continuously all day, every day.
Which line in the code is the Exception pointing to?
Once you know the line number, you can Debug.Log() the values of all the variables accessed on that line. That should take you closer to solving the problem already. That's "debugging 101" :)
hit.collider.gameObject.GetComponent<damageHandler().RocketAlert();
Is not syntactically correct - you're missing the closing angle bracket after damageHandler.
nose kills: i will try that... tanoshimi: that was a typing error here in the forum in the code its right... landern: yep it does -.- more info about the code and error below...
Answer by JannickBremm · Jun 07, 2014 at 02:40 PM
sure here is the hole code...
rocket script:
using UnityEngine;
using System.Collections;
public class rocket : MonoBehaviour
{
RaycastHit hit;
Vector3 fwd;
void Start ()
{
fwd = transform.TransformDirection(Vector3.forward);
}
void Update ()
{
rigidbody2D.AddForce(Vector2.right * 800 * Time.deltaTime);
if(Physics.Raycast(transform.position, fwd, out hit))
{
hit.collider.gameObject.GetComponent<damageHandler>().RocketAlert();
}
}
}
AI script (enemy):
using UnityEngine;
using System.Collections;
public class AI : MonoBehaviour
{
public GameObject launcher;
public GameObject gun;
Vector3 fwd;
RaycastHit hit;
void Start ()
{
fwd = transform.TransformDirection(Vector3.forward);
}
void Update ()
{
if(Physics.Raycast(transform.position, fwd, out hit))
{
Debug.Log(hit.collider);
if(hit.transform.tag == "Player")
{
Debug.Log("hit player");
Instantiate(gun,launcher.transform.position,Quaternion.identity);
}
}
}
}
The syntax error was a copy and paste mistake here in the forum in the code its all written right...
The Error is pointing to following line in the rocket script:
hit.collider.gameObject.GetComponent<damageHandler>().RocketAlert();
The exact error message says: "NullReferenceExeption: Object reference not set to an instance of an object rocket.Update() (at Assets/Scripts/rocket.cs:20)
Your answer
Follow this Question
Related Questions
Errors that I don't know how to fix 1 Answer
Line Renderer not showing 0 Answers
Help!Error CS0266 0 Answers
Object reference not set to an instance of an object 1 Answer