- Home /
very simple shoot script keeps getting error!!!
i made a very simple shoot script to try and shoot a fireball when I clicked, but for some reason I keep getting an error no matter what I do!
script:
var fireballPrefab : Rigidbody;
function Update () {
if(Input.GetButtonDown("Fire1")){
var fireballInstance = Instantiate(fireballPrefab,
GameObject.Find("magicspawn").transform.identity,
GameObject.Find("Main Camera").transform.rotation);
}
}
Error:
NullReferenceException: Object reference not set to an instance of an object fireballshoot.Update () (at Assets/fireballshoot.js:7)
Lo0Nuhti$$anonymous$$ was right, as soon as I deleted transform.identity, it quit giving me the error and started shooting.
Answer by Lo0NuhtiK · Dec 26, 2011 at 12:00 AM
I'm not sure that "transform.identity" is anything ... I could be wrong though. Try 'transform.position' instead.
var fireballPrefab : Rigidbody ; var magicSpawn : Transform ; //drag your spawner into this slot var cam : Transform ; //drag your camera here ; could also be set with Camera.main I suppose var fire : String = "Fire1" ;
function Update(){ if(Input.GetButtonDown(fire)){ ShootOrSomethin() ; } } function ShootOrSomethin(){ var fireballClone : Rigidbody ; fireballClone = Instantiate(fireballPrefab, magicSpawn.position, cam.rotation) ; }
NullReferenceException: Object reference not set to an instance of an object ts2.ShootOrSomethin () (at Assets/ts2.js:16) ts2.Update () (at Assets/ts2.js:10)This is the error I just got right now after having set the code I posted above to instantiate at
(fireballPrefab, magicSpawn.identity, cam.rotation)
This Error Does NOT happen when it is instantiated at
(fireballPrefab, magicSpawn.position, cam.rotation)
Notice the line difference of magicSpawn.identity , and magicSpawn.position .
Both tests were ran just now WITH a rigidbody prefab assigned in the inspector panel.
it says error at line 16 because in my test script I had 2 empty lines at the top before I pasted the code above into it, in case anyone is wondering.
Answer by Siegeon · Dec 26, 2011 at 02:51 AM
NullReferenceException: Object reference not set to an instance of an object fireballshoot.Update () (at Assets/fireballshoot.js:7)
This error message, like JerryCic mentioned has more to do with the fact that what ever reference you are using for you fireball is either empty or unassigned.
Answer by JerryCic · Dec 26, 2011 at 01:31 AM
Or... Are you passing the FireballPrefab into your routine? You do this by inspecting the game object that the script is attached to. In the instpactor you should see the placeholder for the fireballprefab. look in your prefab folder an drag the prefab in question into that placeholder slot.
Your answer
Follow this Question
Related Questions
Shooting script issue? 0 Answers
"Object Reference not set to an instance of an object" 1 Answer
Script keeps functioning when disabled 1 Answer
NullReferenceException Error 2 Answers
Instanciate Object Problem for Shooting 2 Answers