Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by dragonboy77 · Mar 15, 2014 at 09:53 PM · prefabexplosionmine

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;
         }
     }
 }
 


Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MousePods · Mar 15, 2014 at 10:10 PM 0
Share

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");

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dragonboy77 · Mar 16, 2014 at 02:08 AM 0
Share

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!

avatar image MousePods · Mar 16, 2014 at 02:22 AM 0
Share

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. ^_^

avatar image dragonboy77 · Mar 16, 2014 at 03:41 AM 0
Share

Wow!

You are too kind: thank you!

Here it is... I hope its ok.

Download Link

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! :)

avatar image MousePods · Mar 16, 2014 at 04:50 AM 0
Share

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!

Dropbox DL Link

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!)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Communicating between objects in the same prefab? 1 Answer

Explosion at mouse 1 Answer

Add force to an explosion... 2 Answers

Cast Source Instantiating Error 1 Answer

Explosion prefab spawns only the first time in my published app, but spawns on every collision inside Unity Game mode. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges