Question by
BelsonsanPT · Jul 03, 2016 at 01:55 AM ·
waitforsecondsdestroygameobjectsecondsdestroyedsecond
Problem after 5 seconds destroy/deactivate component
I Have a problem...
I need to now how i deactivate this| gameobject.addcomponet(); after 5 seconds. I can´t fix that problem, pls Help!
problem.png
(7.5 kB)
Comment
In the future please to not post images of your code.
Copy your code from your editor and paste it into your question using the 101/010 button above.
If you want more information please look at the Posting Questions, Answers and Comments section in the users guide.
Answer by RyanDiCenzo · Jul 03, 2016 at 05:11 PM
@BelsonsanPT you could use a coroutine to accomplish this. This code is untested so let me know if it doesn't run.
using UnityEngine;
using System.Collections;
public class PowerUp2X : MonoBehaviour
{
public GameObject Player;
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.name == "PowerUp")
{
Player.AddComponent<TapToMove>();
StartCoroutine(RemovePowerup(5.0f));
}
}
IEnumerator RemovePowerup(float WaitTime)
{
yield return new WaitForSeconds(WaitTime);
Player.RemoveComponent<TapToMove>();
}
}