- Home /
Weird NullReferenceException
Hey Guys/Gals, Down here is a script in which I spawn projectiles from a ProjectilePool(Object Pooling). It is attached to a space vehicle, which has a child called 'Tip'.
void Start ()
{
pool = GameObject.FindGameObjectWithTag ("ProjectilePool").GetComponent<ProjectilePool> ();
if(pool.poolSize <=10)
pool.poolSize +=10;
go = pool.RetrieveInstance();
if(go)
{
var script = go.GetComponent<MoveForward>();
speed = script.speed; //retrieves common speed of projectiles
}
foreach(Transform t in transform)
{
if(t.gameObject.name=="Tip")
{
tip = t;
Debug.Log(tip.name);
}
}
}
public void Update()
{
}
public void Fire ()
{
go = pool.RetrieveInstance ();
Debug.Log(tip);
var ray=new Ray(tip.position, tip.forward); \\NullReferenceException
if (go)
{
go.transform.position = tip.position;
go.transform.rotation = tip.rotation;
if(Physics.Raycast(ray, out hit, 100))
{
var time = hit.distance / speed;
StartCoroutine(CastRay(time));
}
}
}
I've attached this script to a turret and it works fine, but on this new space car(still finding a name) it gives a NullReferenceException error at the aforementioned line, and Debug.Log(tip); displays Null at runtime. But Debug.Log(tip.name); displays "Tip". It's like 'tip' is losing it's value/reference between Start() and Fire(), which is called from another script. What do I do?
try this line after the foreach in the start:
if(tip == null){
Debug.Log("could not find tip");
}
Are you assigning tip
as a variable at the start of your script?
Yes, I find any Tip
in the game object and I assign it to t
. Is there anything wrong with that? Thanks for the help, taxvi and StaticHS.
Answer by varunvp · Jan 05, 2015 at 10:16 AM
This might seem dumb guys, but I just had to restart Unity. It works as expected now. Oh, the mysterious ways of the personal computer.....
Answer by hav_ngs_ru · Dec 31, 2014 at 12:08 PM
I guess, possible reasons are:
you could rewrite the tip to null anywhere else in your script; did you post all your script?
you could rewrite tip from another script; try to declare tip as {get; private set;} to avoid assigning it from outside (trying do this will cause compiler error);
you could call Fire earlier then Start (it could be if object is inactive); check Debug.Log order (Start - first, Fire - after);
less possible are:
do you have only one projectile launcher in scene? if more thet one - could it be that you have initiated only first of them, and trying to launch projectile from another? Try to attach name to your Debug.Log in both cases to check this...
there could be two scripts attached to one GO by mistake; it's very silly but I really had such mistake several times :)
cant see other reasons yet... try to check them all first...
Hey hav_ngs_ru, I just had to restart Unity for it to work, however I did try out your solutions. I don't assign tip = null anywhere, and I don't feel the need to rewrite it from another script. Ordering is fine too, Start is executed once first and then "Fire,Fire,Fire,......" is executed.
However, I do have 2 projectile launchers in the scene, which may increase later. I have attached the names of the objects and both of them show up. But I am worried if this can give any problems later since they use only one projectile pool. Thanks again:-)
Your answer
Follow this Question
Related Questions
how to set Object reference to an instance of an object 1 Answer
Null Reference Exception Error...specific error. 1 Answer
Troubleshooting desktop build receiving unhandled NULL exception 0 Answers
Debugging NullReferenceExceptions in iOS Games 2 Answers
Null Reference Problem - Emergency - Help! (same prob/diff variables) 2 Answers