Exploding Mine Prefab not disabeling mesh renderer...

Hi all,

I’m a beginner and I just started learning about scripting. I created an exploding mine based on this tutorial

I made the mine according to the tutorial and everything worked as instructed. However, when I make a prefab and bring more than one mine into the scene only the first mine prefab I place in the scene gets destroyed.

The other ones go “off”, make the particle effect and sounds, but the mesh remains. Also if I trigger a mine prefab I placed in my scene (after the first one) if I step on any other mine in the scene the particles will go off and the first prefab placed on the scene disappears (as its supposed to when triggered) even though it wasn’t the mine that was triggered…

If I only have one mine in the scene everything works as planned. If I add more than one mine (dragging the prefab into the scene from asset folder) that’s when the error occurs. Its all the same prefab which is why I don’t understand what is going on lol.

I also get this error:
“NullReferencEexception Object reference not set to an instance of an object”

Below is the script attached to the particle system (as instructed in the video):

Thank you for any help! :slight_smile:

using UnityEngine;
using System.Collections;

public class MineExplode : MonoBehaviour 
{
	public AudioClip Hurt;
	public AudioClip Explosion;
	private GameObject Mine;
	
	void Start () 
	{
		particleSystem.Stop();
	}
	
	
	void OnTriggerEnter (Collider other) 
	{
	if (other.tag == "Player")
		{
			particleSystem.Play ();
			audio.PlayOneShot(Hurt);
			audio.PlayOneShot(Explosion);
			Destroy (particleSystem);
			Mine = GameObject.Find ("Mine");
			Mine.GetComponent<MeshRenderer>().enabled = false;
		}
	}
}

Instead of doing this

Mine = GameObject.Find ("Mine");
Mine.GetComponent<MeshRenderer>().enabled = false;

Do this instead

transform.GetComponent<MeshRenderer>().enabled = false;

or

this.renderer.enabled = false;

GameObject.Find (“Mine”); will look through the entire scene and give you the first object it finds with the name “Mine”. In your case it will be the first instance of the prefab you added. What you actually want to do is use the instance of the prefab that the script is attached to.