- Home /
Instantiate Problem
Hey guys, I have a huge problem which is stopping me from continuing with my game. I'm trying to make it, so when you press the space bar, it fires a fireball from a spawnpoint. Here's the script:
var bullitPrefab:Transform;
function Update () {
if(Input.GetKeyDown(KeyCode.Space)){
var bullit = Instantiate (bullitPrefab, GameObject.Find("spawnPoint").Transform.position, Quaternion.identity);
}
}
I have set the 'bullitPrefab'to my fireball, but it just says it's a 'NullReferenceException.' What have I done wrong?
Answer by aldonaletto · Aug 10, 2011 at 08:11 PM
Write transform in lower case in the Instantiate instruction (Transform is a type, but transform is the variable you need to use):
var bullit = Instantiate (..., GameObject.Find("spawnPoint").transform.position,...);
Answer by Blankzz · Aug 10, 2011 at 08:23 PM
Is your spawnPoint an object in the scene? If it is check that it is called spawnPoint because you are trying to find it by name. To see if that is the problem comment out the instantiation line and put
Debug.Log(GameObject.Find("spawnPoint"));
If you get a null reference you know its a problem trying to access the spawnPoint.
Your answer
Follow this Question
Related Questions
how to spawn on random postion but non on static positions 0 Answers
Throwing Spheres 1 Answer
create a plain GameObject through script 2 Answers
invokerepeating stops suddenly 0 Answers
Setting parent of instantiated sprite 2 Answers