Hi,
I have 2 prefabs in game, one called boxy, and one called spawner.
I have a scene with a platform with one boxy on it, and a spawner above it.
I want to be able to spawn a boxy from out the spawner.
I have tried a lot of codes and tried modifying a lot of stuff, but it still won’t work:
function Update () {
var boxy: GameObject;
var spawner : GameObject;
if (Input.GetButton("Fire1")){
Instantiate(boxy, Vector3(0,0),Quaternion.identity);
}
}
I know it looks like shit, so, how?
I’m really trying to understand the codes.
Error: NullReferenceException: The prefab you want to instantiate is 0.
I’ve looked that up, but didn’t help.
Help!
“boxy” and “spawner” are the name of your game object. They are strings.
var boxy : GameObject and var spawner : GameObject : they are variables of type GameObject, and called boxy and spawner. They are irrelevant to the previous game object. You could have named those variables in another way, it wouldn’t have increased nor reduced their relevancy level with game object “boxy” and “spawner”…which is already none (the relevancy).
When you declare variables inside a function, they only live inside that function. So, whenever Update is called, temporary variables are created (whose content can be accessed by writing boxy and spawner).
You’d better declare those variables outside of any function, so they last as long as the game runs, and in addition, you can set them up in the Inspector before play (allowing you to link those variables to game objects).