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 CrazyChloz · Feb 22, 2017 at 08:32 AM · destroy objectfunctionsrespawninghealth-deduction

How do you get a function to 'check' a previous function has occurred in order to trigger a second function?

Hello. I'm trying to figure out how to 'check' if a function has occurred so that it triggers the next function.

The basics are that my script either 'collects' (common box) when the player collides with the object, or 'blows up' if the player collides with a bomb. I've got two pieces of code, one which is placed on the player, and another on the camera.

What I was attempting to do with the second script is to check to see if the 'Destroy' function has occurred for the player, and if so, go through the revive code to check whether or not to respawn or Game Over.

This is my first Unity project and so far I've been going through tutorials online and the forums, but have drawn a blank on this particular issue.

Code on Player:

 void OnCollisionEnter2D(Collision2D col)
         {
             if (col.gameObject.name == "CommonBox(Clone)")
             {
                 Destroy(col.gameObject);
             }
             else if (col.gameObject.name == "bomb")
             {
                 Destroy(gameObject);
             }
 

Code on Camera:

 private void revive ()
     {
          if (player == false)
         {
             if (currentHealth >= 1)
             {
                 Instantiate(player, new Vector3(7, 2, -1), Quaternion.identity);
                 currentHealth = currentHealth - 1;
             }
             else if (currentHealth <= 0)
             {
                 Instantiate(gameover, new Vector3(0, 0, -4), Quaternion.identity);
             }
         }
     }
        

Hopefully this makes some sense, if I can provide any more information then please let me know.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by RobAnthem · Feb 22, 2017 at 08:57 AM

Honestly, when it comes to things like this. There is always ways of doing it, I could tell you 3 different ways, but Unity already addressed the entire issue. The simplest answer is Events. Create an event, and subscribe to it.

Here is some Unity docs on the subject.

Events

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Lose health and respawn after falling 2 Answers

Problem with health deduction 1 Answer

Display Hit Text On Collition 1 Answer

How do I call a function containing a while loop, inside the update function? 2 Answers

How to check if a function is null? 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