- Home /
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! :)
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;
}
}
}
Where is this scripted placed? I personally would place this one the $$anonymous$$e itself and just use this.renderer.enabled = false;
I might be because you are using this bit of script to find the $$anonymous$$e. It will always find the same $$anonymous$$e in the scene, I think, and never find the other $$anonymous$$es.
$$anonymous$$ine = GameObject.Find ("$$anonymous$$ine");
Answer by MrCrinkle · Mar 15, 2014 at 10:15 PM
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.
First of all thank you both VERY much for your quick replies!!
Both suggestions did not seem to work unfortunately but I'm sure it must be my set up. I get an error stating:
NullReferenceException: Object reference not set to an instance of an object.
Its referring to this line: particleSystem.Stop();
$$anonymous$$aybe I wrote the script wrong?
using UnityEngine;
using System.Collections;
public class $$anonymous$$ineExplode : $$anonymous$$onoBehaviour
{
public AudioClip Hurt;
public AudioClip Explosion;
private GameObject $$anonymous$$ine;
void Start ()
{
particleSystem.Stop();
}
void OnTriggerEnter (Collider other)
{
if (other.tag == "Player")
{
particleSystem.Play ();
audio.PlayOneShot(Hurt);
audio.PlayOneShot(Explosion);
Destroy (particleSystem);
transform.GetComponent<$$anonymous$$eshRenderer>().enabled = false;
}
}
}
I attached the script to the "$$anonymous$$e" game object this time as suggested. Now the explosion goes off right at play start (without being triggered) and Unity pauses it right away (mid explosion) because of the above error.
I should mention I was using a cylinder scaled down on the Y axis as a placeholder for the $$anonymous$$e - could this have anything to do with it?
Thank you again for your generous help!
Do you think you could package it up and add it as an atttachment? I can take a look at it! I can figure it out faster with it in front of me. ^_^
Wow!
You are too kind: thank you!
Here it is... I hope its ok.
I provided a download link as the uploader wouldn't allow me to attache it here. If the link doesn't work I'll use another method
Thank you again! :)
Np! :)
I did it a little differently, I hope that is ok!
Ins$$anonymous$$d of using public variables, I used the Resource folder. I find it is $$anonymous$$UCH easier then drag + drop especially when you have a lot of assets you want to load in. I explained how to use it with comments.
Also, I instantiated the particle system ins$$anonymous$$d and created a DestroyParticleSystem script which will destroy the particle system automatically when its done. This way you don't have a particle system on every $$anonymous$$e, its only there when you step on it.
I made sure to comment everything. If you get stuck just msg me here again!
I hope you don't get mad, but it is just how I would do it, and I think it works better =] imo
I hope it helps!
P.S. I dont know if you have looked into Playmaker, but as of today its 35 bucks from 95. I DL it for fast prototyping and a visual way to debug and code. Its great for new scripters too as it will help you learn unity and code! (at least from comments and reviews iv read! - still need to try it but have to finish this game im working on first!)