I´m trying to do a small ships games but i get this error:
NullReferenceException: The prefab you want to instantiate is null.
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation)
Movimiento_nave.Disparar () (at Assets/Movimiento_nave.js:34)
Movimiento_nave.Update () (at Assets/Movimiento_nave.js:19)
Whith this code:
var speed = 6.0;
var bala : Transform;
var proteccion : Transform;
private var moveDirection = Vector3.zero;
function Update() {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if(Input.GetButtonDown("Fire1"))
{
Disparar();
}
/*
if(Input.GetButtonDown("Fire2"))
{
Proteger();
}
*/
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
}
//Function to fire
function Disparar()
{
var Bala : Transform = Instantiate (bala,transform.position,transform.rotation);
Bala.rigidbody.velocity = Bala.forward * speed * (-1);
}
/*
function Proteger()
{
Instantiate (proteccion,transform.position,transform.rotation);
proteccion.transform.parent = transform;
}
*/
@script RequireComponent(CharacterController)
Where is the problem? I´m a noob, this is my first script.
You probably didn’t drag a prefab to the ‘proteccion’ slot on your script in the inspector.
I have a prefab on proteccion in the inspector. But i´ll disable this var as i´m not using it at this moment. The problem should be somethink on the code.
You can’t use the prefab like it’s an object. You need to create a temporary variable to hold the instantiated object and assign it its parent.
So, all you have to do is switch:
Instantiate (proteccion,transform.position,transform.rotation);
proteccion.transform.parent = transform;
with this:
var temp : Transform = Instantiate (proteccion,transform.position,transform.rotation);
temp.transform.parent = transform;
Edit: Of course, that won’t solve the error, as that says you’re instantiating an empty variable. Either bala or proteccion must be empty when instantiated in order to create this error.
The problem should be on disparar function as lines shown as error are the function call on update and the instantiate line on disparar function. This scrip is attached to the spaceship so i create bullets on its position.
Well, then the variable bala must be empty when you try to instantiate.
The error specifically says that you’re instantiating an empty object, so check to make sure bala has a value when the function is called.
Bala (Bullet) is a bullet prefab. It´s ok on the inspector so variable is not empty.
Have you tried checking during runtime?
Comment out all instantiate functions and see if you still get the error, as it’s obviously the fault of an empty prefab.
I comment out all instantiate functions and all it´s ok. It´s a small code so it´s easy. I get the error but i don´t have any problem on the game. Bala (Bullet) instanciates correctly and all work as i want.