- Home /
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;
}
}
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
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.
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);
}
}
}
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
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.
Updated my answer based on your previous comment
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
I'm glad I was able to resolve. Can you please accept this solution as correct, if it solve your issue. Thanks
Your answer
Follow this Question
Related Questions
Need help figuring how to add more time to timer 1 Answer
Certain timers run too fast 1 Answer
A Split Second Timer; 4 Answers