- Home /
Nullreference?
I am trying the following:
Whenever the spacebar is pressed, a Thunderball spawns from the opponents head (through a sphere spawnpoint) and add a force to move down, like a thunder strike
i have been coding for a couple of minutes and already have a problem
// Cast Thunder on the Cube
if(Input.GetButtonDown("Jump"))
{
var ThunderBullet = Instantiate(ThunderBullet, GameObject.Find("SpawnPoint").transform.position, //something to cast the thunder to go down// );
}
Here is the code; I get the Nullreference error (object reference not set to an instance of an object)
Here is what i'm using to achieve this:
Cube (Opponent, Standing still)
Sphere (SpawnPoint, child of ^Cube^)
Capsule (Character)
ThunderPrefab (The Thunderball)
var Thunderbullet:Transform; (on the script)
Can anyone help me here? I am equal to a sloth when it comes to coding
Answer by CHPedersen · Mar 25, 2013 at 11:17 PM
Either the prefab for ThunderBullet has not been set (typically done through the editor) or a GameObject by the name "SpawnPoint" does not exist in the scene at the moment this code is run.
My guess is the latter, because I think nullreferences from unset prefabs trigger a special message about the prefab being null. So it's probably the SpawnPoint. Put it in a call to Debug.Log before trying to use it and see what it returns.
I'm trying like crazy, i can't find what's wrong with the code
Spawn is called spawn, and the prefab is set (Yes, i redid the code by scratch, hoping i got something wrong before)
Be literal when referring to GameObjects and their names. When you say "Spawn" is called "spawn", is that what you literally mean? Because then that's your error. GameObject.Find is case sensitive. If the one you want is called "spawn", then GameObject.Find("Spawn"), with a capital S, will return null.
no that was a mistake by me
Spawn is Literally called Spawn
Okay. GameObject.Find should find that. I can see why that might be confusing. Thankfully, the code you've posted is so wonderfully simple that it must be either the call to GameObject.Find or the prefab which is null. There is no other place in that script where an object is used in a manner that can throw a NullReferenceException. Thus, as suggested before, you need to put calls to Debug.Log before the call to Instantiate to ascertain which one it is. Do this:
// Cast Thunder on the Cube
if(Input.GetButtonDown("Jump"))
{
Debug.Log("State of Thunder: " + Thunder);
Debug.Log("State of Spawn: " + GameObject.Find("Spawn"));
var Thunder = Instantiate(Thunder, GameObject.Find("Spawn").transform.position, Quaternion.Identity);
}
And pay attention to what the log says. One of them is null. If the log stays empty, then this code is never even called and your exception is thrown elsewhere.
Your answer
Follow this Question
Related Questions
Need Help with Shooting Script c# 5 Answers
2D Sending Object From One Place To Mouse Position. 1 Answer
My object acts wierd, 2 balls being shot instead of one (from different position) 2 Answers
Gun Script 1 Answer
Pre-Normalized cube model? 3 Answers