- Home /
Particles not Instantiating : NullReferenceException
Ok, so below is the script that is giving me trouble. I'm pretty sure i've followed the instructions given on the "Instantiate" reference, but i keep getting a "NullReferenceException: Object reference not set to an instance of an object" error, on the line where i give the Instantiate instruction.
What object do i need to instantiate? is it not properly instantiated?
Thanks ahead for any help, it will come in very valuable.
public var maxHealth = 100;
var currentHealth = maxHealth;
public var explosionObj : GameObject;
function Start(){
}
public function Damage (dmg : int){
currentHealth = currentHealth - dmg;
if (currentHealth == 100){
}
if (currentHealth >= 75 && currentHealth < 99){
}
if (currentHealth >= 50 && currentHealth < 74){
}
if (currentHealth >= 25 && currentHealth < 49){
}
if (currentHealth > 0 && currentHealth < 24){
}
if (currentHealth < 0){
if (gameObject.name == "Player_0")
Application.LoadLevel("teste leve");
else{
Debug.Log("i should be dead");
var explosion : GameObject = Instantiate(explosionObj, gameObject.position, gameObject.rotation);
Destroy(gameObject);
}
}
}
Are you sure, that the variable "explosionObj" actually has the "Explosion Prefab" in it's correct slot, in the inspector?
I am positive, however, what i did was to create a Particle System an then put it into a prefab. I then put this prefab in the inspector slot. Is this wrong?
That is absolutely NOT wrong :) A particle system, is a GameObject, just like anything else in your scene.
Something else funny happens. $$anonymous$$y "hero" shoots bullets at the character where this script is attached. When the "currentHealth" mentioned above goes below zero, the bullets no longer disappear, as they did until that point, and just weirdly bounce off him.
Is this script attached to the enemy? If so, to make the bullet disappear you need to attach a script to bullet to make it destroy itself on collision with enemy. However, I do not know the solution to your original problem. Did you do these steps?
Create a particle system. Tweak it.
Create new prefab.
Drag and drop the create particle system object to the prefab.
Drag and drop the prefab to the script linked to the gameObject in the scene.
Did you follow these steps exactly?
Answer by dkfister · Jan 15, 2014 at 03:49 AM
I'm pretty sure, gameObject doesn't have a position and a rotation alone by itself. These variables, come from the component ON the gameObject, which is transform.
So you have to change this line:
var explosion : GameObject = Instantiate(explosionObj, gameObject.position, gameObject.rotation);
To:
var explosion : GameObject = Instantiate(explosionObj, transform.position, transform.rotation);
Thank you, man, you're awesome! It worked. Not quite sure why i used gameObject, i guess i didn't give it much thought! I can't up vote your answer because my rep is below 15, but thank you so much.