Disable script after time
I have a powerup that enables shooting script that is on my player object. powerup script is on my powerup object. Now when I enable script using
player.GetComponent<ShootingRocket>().enabled = true;
it works fine but when I try to disable script after 5 sec it doesnt do anything. Here is script that I used for that :
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
StartCoroutine(Pickup(other));
}
}
IEnumerator Pickup(Collider2D player)
{
player.GetComponent<ShootingRocket>().enabled = true;
GetComponent<SpriteRenderer>().enabled = false;
GetComponent<Collider2D>().enabled = false;
yield return new WaitForSeconds(sec);
player.GetComponent<ShootingRocket>().enabled = false;
Destroy(gameObject);
}
I have made a variable for time :
public float sec = 5f;
I dont know what i am doing wrong. If anyone can help please do.
Answer by Navin_Nexusviper · Aug 14, 2020 at 06:23 PM
According to my understanding there is some part of the code which is getting triggered and making the Shooting Rocket enabled again. The code snippet provided by you looks fine to me. problem is somewhere else.
Thank you for the response. I am not using coroutines or GetComponent anywhere else. What can cause problem to this script? Where do I need to look? because everything else works. GameObject is destroyed after set amount of seconds, but disableing the script doesnt work. Any idea where can the problem be?
Your answer
Follow this Question
Related Questions
Need help disabling a script in another GameObject 1 Answer
Animation only plays once 0 Answers
How to create a mesh at a location with code? 1 Answer
Movement script preventing gravity 0 Answers
Creating Objects on Right Click to Navigate Around 0 Answers