Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 martipello · May 09, 2017 at 10:33 PM · gameobjectdestroyparticlesystemmeshrenderer

particle system effects other things in my game

i currently have a piece of code that destroys some gameobjects in a group and instantiates coins in their place, it looks like this

   GameObject[] sensedObjects = GameObject.FindGameObjectsWithTag("THREE_BY_THREE_GROUP");
             foreach (GameObject foundOne in sensedObjects)
             {
                 GameObject objectMain = foundOne.transform.parent.gameObject;
                 GameObject newObj = GameObject.Instantiate(coins) as GameObject;
                 newObj.transform.parent = objectMain.transform.parent;
                 Vector3 lower = new Vector3(objectMain.transform.position.x, 
                 objectMain.transform.position.y - 0.5f, objectMain.transform.position.z);
                 newObj.transform.position = lower;
                 newObj.transform.localScale = new Vector3(0.15f, 0.15f, 0.16f   );
                 
                 GameObject.Destroy(objectMain);

             }

this works perfectly but what im trying to do now is when this happens (game object object main being destroyed and game object newObj being instantiated) is show a particle system looking like an explosion and disapearing after its finished so i made a particle system in the editor on my object main and tried to use the following code

    GameObject[] sensedObjects = GameObject.FindGameObjectsWithTag("THREE_BY_THREE_GROUP");
             foreach (GameObject foundOne in sensedObjects)
             {
                 GameObject objectMain = foundOne.transform.parent.gameObject;

                 ParticleSystem ps = objectMain.GetComponent<ParticleSystem>();
                 ps.Play();
                 objectMain.GetComponent<Renderer>().enabled = false;
                 objectMain.GetComponent<Collider>().enabled = false;

                 GameObject newObj = GameObject.Instantiate(coins) as GameObject;
                 newObj.transform.parent = objectMain.transform.parent;
                 Vector3 lower = new Vector3(objectMain.transform.position.x, 
                 objectMain.transform.position.y - 0.5f, objectMain.transform.position.z);
                 newObj.transform.position = lower;
                 newObj.transform.localScale = new Vector3(0.15f, 0.15f, 0.16f   );
                 
                 
                 GameObject.Destroy(objectMain,ps.main.duration);

             }

this has bad results the particle system plays, the renderers and coliders are off so that it disappears instantly and is destroyed after the particle system finishes, but the newly instantiated coins kind of flash with the particle system for a brief second and then is destroyed (isnt appearing in the scene hierarchy) im quite new to unity can anyone help?

Comment
Add comment · Show 5
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 RobAnthem · May 10, 2017 at 01:53 AM 0
Share

This seems like an extremely convoluted way to accomplish a very simple task. What are the events the lead up to this destruction/instantiation?

avatar image bobisgod234 · May 10, 2017 at 05:30 AM 0
Share

Put a debug.log before destroying the gameobject and output the value of ps.main.duration. I suspect its a very small value, and is causing it to be destroyed in a fraction of a second.

avatar image martipello · May 10, 2017 at 08:01 AM 0
Share

@RobAnthem all objects are passed through once, when all objects in a group have been passed through they're all turned into (destroy / instantiate) into coins and as mentioned works perfectly without the particle system, @bobisgod234 I have printed a line to see it fire, and the particle system should be 1 second long but I haven't printed it to check so I'll do that and update thanks for your answers

avatar image RobAnthem martipello · May 10, 2017 at 08:11 AM 0
Share

I get that, but what I mean is, let's say this event is triggered by a collision, then almost all of this script is useless. Searching and iterating is something that should be done on extreme circumstances, what I want to know is what you are actually ai$$anonymous$$g to accomplish here. If an object hits these objects and they turn into coins, then you went about doing this wrong. If your particle system only triggers once per object, consider placing it on the coin and let it play on awake. Disabling the renderer and collider makes no sense, you are adding additional overhead to the simple task of disabling the object itself. Remember that FindObject and GetComponent calls are costly and should be avoided, actually there is almost never a reason to need them except in rare occasions.

To make matters worse, your system almost reflects object pooling, which is s good thing, but then you follow it up with a timed destroy, adding additional overhead and forcing garbage collection.

avatar image martipello · May 10, 2017 at 11:07 AM 0
Share

@RobAnthem I'm ok at program$$anonymous$$g so most of what you said makes sense but I'm near clueless with unity so your comment gives a small insight into expensive calls such as find so thank you, I don't quite understand why if this is a collision then all of this is useless, is that because your assu$$anonymous$$g this script will be on the object (it's actually on the player) but what you mentioned about putting the particle system on the coin and play in awake is exactly what I should be doing and what j will be doing going forward if you want to add that as an answer I'll accept it, many thanks for all your words

0 Replies

· Add your reply
  • Sort: 

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

90 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 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 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 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 avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

How to destroy particle gameobject after its finished? 2 Answers

Why Destroy(gameobject) fail once in a while? 2 Answers

"Some objects were not cleaned up when closing the scene" 1 Answer

Destroy all Objects of a Type in Game 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