- Home /
NullReferenceException with a turret shooting a bullet
Ok i followed a tutorial on yotube: http://www.youtube.com/watch?v=fMUpJNndzcY
they thougt me how to rotate a turret towards the player n stuff. But now i'm trying to get the turrets to shoot the player. The scripting is the same as the guy on the tutorial(yeah i checked like 50 times). I've added a rigidbody, spawnpoint and a bullet. And i've added a delay to the shooting for like 1 second or something. And when i play the game i can play it for0,9 seconds then the bullet comes out of the turret and stands still in front of it giving me this error
NullReferenceException Turretcontrol.Shoot () (at Assets/Turretcontrol.js:29) Turretcontrol.Update () (at Assets/Turretcontrol.js:21)
Please help, i've already wasted a few hours of my life trying to solve it...
Answer by Kasper · Jan 28, 2011 at 01:34 PM
Here it is :D, Ok i see that the code got really wierd somehow.
var LookAtTarget : Transform; var damp = 6.0; var bullitPrefab:Transform;
function Update() {
if(LookAtTarget) {
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
var seconds : int = Time.time;
var oddeven = (seconds / 2);
if(oddeven)
{
Shoot();
}
}
}
function Shoot() { var bullit = Instantiate(bullitPrefab , transform.Find("spawnpoint").transform.position , Quaternion.identity);
bullit.rigidbody.AddForce(transform.forward * 1000);
}
Does your "bullitPrefab" have a rigidbody? Also, which are line 21 and 29?
Also, does transform.Find("spawnpoint") return a transform? or null? transform.Find search for the transform inside his children. something like GameObject.Find("spawnpoint") would be more appropriate if the spawnpoint isn't child of the current object.
yes it does and the eroor has move to only line 29 so thats were tha fault is
function Shoot() { var bullit = Instantiate(bullitPrefab , transform.Find("spawnpoint").transform.position , Quaternion.identity);
bullit.rigidbody.AddForce(transform.forward * 1000);
}
Call Instantiate(bullitPrefab, transform.Find("spawnpoint").transform.position, Quaternion.identity); At the start of Update() or in Start(). Just to test. If it fails there, try on the Find() part.
Answer by Phil 5 · Jan 28, 2011 at 03:09 PM
var newPrefab : Transform = transform.Find("spawnpoint");
if (newPrefab)
{
newPrefab = Instantiate(bullitPrefab , newPrefab.position , Quaternion.identity);
if (newPrefab)
{
if (newPrefab.rigidbody)
{
Debug.Log("Bug is strange");
}
else
{
Debug.Log("No rigidbody in prefab");
}
}
else
{
Debug.Log("Buggy Prefab");
}
}
else
{
Debug.log("Cant find");
}
Try this and tell me which output it gives. Put this in "Shoot()", and comment your current code in that function.
Assets/Turretcontrol.js(27,18): BCE0043: Unexpected token: var. Assets/Turretcontrol.js(27,22): BCE0044: expecting EOF, found 'newPrefab'. this happened when i put it in the function shoot() when i put it in shoot() it gave me like 6 errors or something
It's synthax error i think, i'm not used to javascript, EOF means End-Of-Fin