Prefab to instatiate is null

I’m trying to get a Detonator explosion to go off, and I’m having trouble getting it work - but it’s probably something to do my scripting!

Hopefully this won’t be too complicated, but… I have a script that instantiates a player model and attaches a player script to it.

That player script should then instantiate the Detonator explosion, but instead it tells me: NullReferenceException: The prefab you want to instantiate is null.

I’ve been searching the forums, but I haven’t come up with anything that helps - I have dragged the Detonator prefab onto the player script in the Inspector.

So here’s the first script that instantiates the player:

var playerFighterPrefab : Rigidbody;
function Start() {
	var playerFighter = Instantiate(playerFighterPrefab, Vector3 (0,0,0), Quaternion.identity);
	// attach the player control script
	playerFighter.gameObject.AddComponent("playerScriptHanger");
}

Here’s the bit of “playerSciptHanger” that should set off the explosion:

var detonatorPrefab : GameObject;
var explosionLife : float = 10;
function OnCollisionEnter(otherObject:Collision){
	if (otherObject.gameObject.tag == "Ground"){
		var exp = Instantiate (detonatorPrefab, transform.position, Quaternion.identity);
		Destroy(exp, explosionLife);
	}
}

If I play the game in Unity, and then click on my player model, the ‘Detonator Prefab’ variable in the Inspector is set to none. If I use the drop down and set it to ‘Detonator-Base’, then the explosion goes off as expected.

Any advice on what to look at would be appreciated.

BTW, is this kind of thing covered in Will’s book? Maybe I should invest in it rather than pester you guys on the forums :slight_smile:

Scripts by default have all variables like game objects and transforms null. If you want to get around that, make a variable that holds it in the script that creates the object with the script. After that, you can just access it directly and assign the variable to it, which would get rid of your error.

Alternatively, you could have the prefab you’re creating the script for have it attached and configured already. It’d probably save you a lot of time.

I’m not sure I understand this. The playerScriptHanger script has the Detonator prefab attached to it already, and the prefab is configured.

Or did you mean I should attach the prefab to the script that calls the playerScriptHanger script?

Just a little bump, because I still haven’t got this working.

One more bump to try and resolve this :slight_smile:

If you can you upload an example script, I’ll look at it for you.

The scripts are pretty much as above. I’ve just created a test project and I’m getting the same result.

A cube stretched out in X and Z to represent the ground. A new tag called ‘Ground’ added and assigned to the cube.

A sphere is created to act as a player. Rigidbody added to it. Following script added to it (playerScript.js):

var detonatorPrefab : GameObject; 
var explosionLife : float = 10; 
function OnCollisionEnter(otherObject:Collision){ 
   if (otherObject.gameObject.tag == "Ground"){ 
      var exp = Instantiate (detonatorPrefab, transform.position, Quaternion.identity); 
      Destroy(exp, explosionLife); 
   } 
}

Detonator-Base from the Detonator Prefab Examples dragged onto the script. Sphere is converted to prefab called “playerFighterPrefab” and then deleted from scene.

An empty GO created, with the following script attached (ControllerScript.js):

var playerFighterPrefab : Rigidbody; 
function Start() { 
   var playerFighter = Instantiate(playerFighterPrefab, Vector3 (0,2,0), Quaternion.identity); 
   // attach the player control script 
   playerFighter.gameObject.AddComponent("playerScriptHanger"); 
}

PlayerFighterPrefab is dropped onto this script.

So when I click Play, the sphere appears, drops to the ground, and then I get the error message.

Inspecting the player object whilst the game is running reveals that ‘Detonator Prefab’ is blank.

If I pause the game before the sphere hits the ground, manually choose ‘detonator-base’ from the drop down, then the explosion appears as expected.

I suspect there’s a some knack to assigning detonator-base to my player object, but I’m not sure what it is :slight_smile:

Are you getting any other error messages in the project before you go into Play mode?

Prefabs and instantiation are indeed covered in Will’s book. You may want to invest in it anyway, but please don’t feel that there’s anything wrong with asking questions on the forum.

No, no other errors. The text from the Editor.Log reads:

I’ve just created a new scene, added an empty GO, and then attached this script to it:

var detonatorPrefab : GameObject; 
var explosionLife : float = 10; 

function Start() { 
	var exp = Instantiate (detonatorPrefab, transform.position, Quaternion.identity); 
      Destroy(exp, explosionLife); 
 }

I’ve dragged a detonator prefab onto the script, run the project and I still get the same error.

If I drop the prefab into the scene, then the explosion plays.

So either there is something wrong with the detonator prefabs that causes them to not like being instantiated, or they need to be instantiated in a specific way.

I’m now running Unity 2.6, and the problem is the same as it was uner the 2.5 trial.

I’m having the same problem. My code is very simple (C#).
First I declare a public variable in my script:

public GameObject cube;

Then in the inspector I select a prefab from the dropdown list created by this public var. The prefab just contains a sphere and nothing else.

Then inside an event listener I try to instantiate this prefab:

Instantiate(cube,new Vector3(0,0,0),Quaternion.identity);

And every time the error:

NullReferenceException: The prefab you want to instantiate is null.

var playerFighter = Instantiate(playerFighterPrefab, Vector3 (0,0,0), Quaternion.identity);
// attach the player control script
playerFighter.gameObject.AddComponent(“playerScriptHanger”);

The above is where the problem is. You’re attaching a fresh script to the gameObject, and that script does NOT have the detonator prefab attached. When you add a script like this, it holds nothing but empty or default variables. So, in order to get around that, you could have playerFighterPrefab have the script attached and configured already, which would eliminate the need for the AddComponent call.

OK, but it seemed odd as I had the prefab added to the script in the Project view. I’m guessing then that when the script is attached to a GO via AddComponent, the prefab is dropped somehow.

I’ve solved it now though. The detonator prefab is attached to the player script (as it was before), and now the player script is attached to the plane prefab.

I’ve duplicated the plane prefab (without the player script/detonator prefab), and added that to the controller script so I can generate additional planes that don’t respond to the players input.

Thanks for the assistance :slight_smile: