This question was
closed Jul 03, 2016 at 06:29 PM by
TBruce for the following reason:
This is a duplicate question
http://answers.unity3d.com/questions/1211137/problem-after-5-seconds-destroydeactivate-componen.html
Question by
BelsonsanPT · Jul 03, 2016 at 03:38 PM ·
destroywaitforsecondsdestroy objectsecondssecond
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
Answer by RyanDiCenzo · Jul 03, 2016 at 04:57 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);
Destroy(Player.GetComponent<TapToMove>());
}
}
I Just Have one erro.
Severity Code Description Project File Line Error CS1061 'GameObject' does not contain a definition for 'RemoveComponent' and no extension method 'RemoveComponent' accepting a first argument of type 'GameObject' could be found
Try this ins$$anonymous$$d of Player.RemoveComponent();
Destroy(Player.GetComponent<TapTo$$anonymous$$ove>());