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?