- Home /
Problem with Instantiate (maybe)
Hello Community, i've a problem with my code. In this code, a canon will shoot bullets.
var LookAtTarget : Transform;
var damping : float = 6.0;
var bullet_Prefab : Transform;
var savedTime = 0;
function Update () {
if (LookAtTarget){
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damping);
var secounds : int = Time.time;
var oddeven = (secounds % 2);
if (oddeven){
shoot();
}
}
}
function shoot(){
if(secounds != savedTime){
var bullet = Instantiate(bullet_Prefab, transform.Find("SpawnPoint").transform.position, Quaternion.indentity);
bullet.rigidbody.AddForce(transform.forward * 1000);
savedTime = secounds;
}
}
Million Errors occur ,when i start the game:
NullReferenceException: Object reference not set to an instance of an object TurnetController.shoot () (at Assets/Scripts/TurnetController.js:23) TurnetController.Update () (at Assets/Scripts/TurnetController.js:16)
I hope you can help me and sorry for my bad English.
Thank you in advance!
Answer by meat5000 · Sep 05, 2013 at 04:11 PM
transform.Find("SpawnPoint").transform.position
This line looks incorrect.
you have transform in the expression twice.
Perform your Find("SpawnPoint") before your instantiate call (and note that Find is expensive) and pass the Find to a variable, say spawnTransform, then use spawnTransform.transform.position.
I think that would work.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
shoot script is not working 1 Answer
Can't convert int to bool? 2 Answers
How to make a smooth transition between Vector3's 2 Answers
Script is not working at all 2 Answers