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 Stef · Dec 08, 2011 at 11:11 PM · destroyobjectscounter

if 20 objects are destroyed...

Lads, I'd like to write a script in which once twenty block are destroyed a particle event is triggered. I'm just having a hard time figuring out the simplest way to write it. Any ideas?

Thx,

Stef

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 gregzo · Dec 08, 2011 at 11:14 PM 0
Share

Increment a blocksDestroyed int when you destroy a block, and check for blocksDestroyed == 20 to instantiate your particle system maybe?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by dannyskim · Dec 08, 2011 at 11:18 PM

Well, the simplest way would just be to increment a static value every time you call the Destroy() on the blocks.

 using UnityEngine;
 using System.Collections;
 
 public class blockCounter : MonoBehaviour {
 
     public static int blocksDestroyed = 0;
 
         void Update()
         {
             if( blocksDestroyed == 20 )
             {
                 // Play particle effect here;
             }
         }
 }

From your other scripts before you call Destroy():

 blockCounter.blocksDestroyed += 1;
 Destroy( gameObject);
Comment
Add comment · Show 5 · 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 aldonaletto · Dec 08, 2011 at 11:39 PM 0
Share

You can also use OnDestroy in the block script - this will work even if other objects destroy the blocks:

   void OnDestroy(){
       blockCounter.blocksDestroyed++;
   }
avatar image Eric5h5 · Dec 09, 2011 at 12:57 AM 2
Share

It's not all that efficient to have an Update loop constantly checking a value which rarely changes, nor is it great practice to use public variables for this (since it could conceivably be accidentally altered). It would be much better to call a function which updates the blocksDestroyed variable and does something.

 int blocksDestroyed = 0;

 public void IncrementBlocksDestroyed ()
 {
     if (++blocksDestroyed == 20)
     {
         // whatever
     }
 }
avatar image Stef · Mar 07, 2012 at 10:11 PM 0
Share

Thx Eric. How does the script know that 'blocksDestroyed' is related to blocks being destroyed in game?... How is the relation created?

avatar image gregzo · Mar 07, 2012 at 11:43 PM 0
Share

@Stef you need to learn basic scripting! You're replying to Eric's answer with a 3 months delay, way enough to learn about how you could implement his suggestion. Everytime you destroy a block, call the IncrementBlocksDestroyed function he suggested, that should be enough to "create a relation"!

avatar image Stef · Mar 07, 2012 at 11:51 PM 0
Share

Actually, I've been away for the last 3 months, and I'm just getting back to this now. Thanks.

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

8 People are following this question.

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

Related Questions

destroy object 2 Answers

Pick up objects to destroy one object. 1 Answer

Destroying objects one at a time 1 Answer

destroy and create objects 1 Answer

Destroy All Objects In The Scene??? 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