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