Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by unity_OwUCpCrs4mGrzQ · Mar 13 at 08:55 PM · countdown

How can I start countdown after OnTriggerEnter2D and change bool check to false

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TripleShot : MonoBehaviour
 {
 
     public static bool check;
 
     public void OnTriggerEnter2D(Collider2D col)
         {
             if (col.tag == "Player")
             {
                 Destroy(this.gameObject);
                 SoundManager.instance.Play(SoundManager.SoundName.tripleShotCollect);
                 check = true;
                 StartCoroutine(tripleShotTimer());
             }
         }
     IEnumerator tripleShotTimer()
     {
         yield return new WaitForSeconds(10f);
         check = false;
     } 
 }

Comment
Add comment · Show 2
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 asafsitner · Mar 13 at 02:42 PM 0
Share

What's the problem here?

As a side note, you should probably not declare check as static, or it will be shared by all of the scripts, and one of them could be changing it to true while others are trying to set it to false. More on the static modifier here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static

avatar image unity_OwUCpCrs4mGrzQ asafsitner · Mar 16 at 07:57 AM 0
Share

I tried to do this but it didn't work. I use static because another script uses bool check for activating something.

I tried to do like when picking up the item then you have power for 10s after that back to normal.

2 Replies

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

Answer by Narc0t1CYM · Mar 13 at 11:11 PM

In order for a collision to happen, you need a BoxCollider2D with Is Trigger set to true and a RigidBody to collide with each other.

Player should have a RigidBody and a BoxCollider2D component attached to it with the below PlayerPowerUp script attached as well

The power-up should have a BoxCollider2D component attached with Is Trigger set to true with the below TripleShot script attached as well

PlayerPowerUp.cs

 using System.Collections;
 using UnityEngine;
 
 public class PlayerPowerUp : MonoBehaviour
 {
     private bool _hasTripleShot;
 
     private void Update() {
         if (_hasTripleShot) {
             // Player can do something here
         }
     }
 
     public void StartTripleShotCountdown() {
         StartCoroutine(TripleShotTimer());
     }
 
     public IEnumerator TripleShotTimer() {
         Debug.Log("Player has Triple Shot power-up");
         _hasTripleShot = true;
         
         yield return new WaitForSeconds(10f);
         
         _hasTripleShot = false;
         Debug.Log("Player doesn't have Triple Shot power-up anymore");
     } 
 }



TripleShot.cs

 using UnityEngine;
 
 public class TripleShot : MonoBehaviour
 {
     public void OnTriggerEnter2D(Collider2D col)
     {
         if (col.CompareTag("Player")) {
             Debug.Log("Colission is happening");
             SoundManager.instance.Play(SoundManager.SoundName.tripleShotCollect);
 
             PlayerPowerUp playerPowerUp = col.GetComponent<PlayerPowerUp>();
 
             if (!playerPowerUp) {
                 Debug.LogError("PowerUpScript is not found on player");
             }
             // Call the player's TripleShotTimer CoRoutine
             playerPowerUp.StartTripleShotCountdown();
             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 unity_OwUCpCrs4mGrzQ · Mar 16 at 08:15 AM 0
Share

I need when player picks up the item, that item should disappear on the screen. So I need to destroy it immediately when player gets it.

About collision, it worked but I need countdown for 10s then back to normal. now I have when the player gets the item, the player will have a special power but my problem is how to countdown for 10s then back player to normal power. So I tried use bool check for countdown it 10s then change it to false. but it didnt work.

:D

avatar image Narc0t1CYM unity_OwUCpCrs4mGrzQ · Mar 16 at 10:32 AM 0
Share

As I understand, this TripleShot is a power-up that the player can pick up. If you want your player to have this power-up for 10 seconds then I'll edit my initial answer that will fit your need.

avatar image Narc0t1CYM unity_OwUCpCrs4mGrzQ · Mar 16 at 10:38 AM 0
Share

Updated my answer based on your previous comment

avatar image unity_OwUCpCrs4mGrzQ Narc0t1CYM · Mar 16 at 10:53 AM 1
Share

It worked!! Thank you so much

Show more comments
avatar image
0

Answer by kaushiknis · Mar 16 at 02:59 PM

The issue here is you are destroying the gameobject on which you are calling the coroutine. You can shift the coroutine code to an active gameobject and that can fix your issue

Comment
Add comment · Show 2 · 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 unity_OwUCpCrs4mGrzQ · Mar 16 at 03:07 PM 0
Share

oh, that's why. thank you very much.

avatar image kaushiknis unity_OwUCpCrs4mGrzQ · Mar 16 at 06:51 PM 0
Share

I'm glad I was able to resolve. Can you please accept this solution as correct, if it solve your issue. 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

135 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 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

Countdown code not working 1 Answer

How to make a coundown of destroyed objects ? 1 Answer

3D number countdown 0 Answers

how to make a varied countdown timer 1 Answer

Timer doesn't work properly 2 Answers


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