how to set gameobject inactive then active after a few seconds in unity?

Viewed 6871

I want to make a game object to be inactive when the player is colliding with a game object (this I have done). now I want to wait for a few seconds and then activate it again. how can I do this?

here is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement: MonoBehaviour
{

private float chubbyScore = 0;
private float coin = 0;
public float speed = 1;
public float jump = 1;

void Update()
{
    Vector3 playerMovement = Vector3.zero;
    playerMovement.x = Input.GetAxis("Horizontal");
    transform.position += playerMovement * speed * Time.deltaTime;
    transform.Translate(Vector3.forward * Time.deltaTime * speed);

    if (Input.GetKeyDown(KeyCode.Space))
    {
        GetComponent<Rigidbody>().velocity = Vector3.up * jump;
    }
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Pick Up"))
    {
        other.gameObject.SetActive(false);
        coin += 1;
        chubbyScore += 50;
    } 

}
}
3 Answers

I would create a public float for the offset duration, then you can create a private float for the timer.

The code to set your objects active state may look something like this:

private float chubbyScore = 0;
private float coin = 0;
public float speed = 1;
public float jump = 1;
public float offsetTime = 2f;
private float timer = 0f;
private GameObject collObj;

void Update()
{
    Vector3 playerMovement = Vector3.zero;
    playerMovement.x = Input.GetAxis("Horizontal");
    transform.position += playerMovement * speed * Time.deltaTime;
    transform.Translate(Vector3.forward * Time.deltaTime * speed);

    if (Input.GetKeyDown(KeyCode.Space))
    {
        GetComponent<Rigidbody>().velocity = Vector3.up * jump;
    }

    if(!collObj.active)
    {
        timer += Time.deltaTime;
        if(timer > offsetTime)
        {
            timer = 0f;
            collObj.SetActive(true);
        }
    }
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Pick Up"))
    {
        collObj = other.gameObject;
        collObj.SetActive(false);
        coin += 1;
        chubbyScore += 50;
    }

}

Create a simple wait universal utility like so:

public class Waiter : MonoBehaviour
{
    static Waiter instance = null;
    static Waiter Instance
    {
        get
        {
            if (instance == null)
                instance = new GameObject("Waiter").AddComponent<Waiter>();
            return instance;
        }
    }
    private void Awake()
    {
        instance = this;
    }
    private void OnDestroy()
    {
        if (instance == this)
            instance = null;
    }

    IEnumerator WaitRoutine(float duration, System.Action callback)
    {
        yield return new WaitForSeconds(duration);
        callback?.Invoke();
    }

    public static void Wait(float seconds, System.Action callback)
    {
        Instance.StartCoroutine(Instance.WaitRoutine(seconds, callback));
    }
}

This automatically injects itself into the game when needed, you just need to create the script, Now in your OnTriggerEnter

 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.CompareTag("Pick Up"))
     {
         collObj = other.gameObject;
         collObj.SetActive(false);
         Waiter.Wait(3, () =>
         {
             // Just to make sure by the time we're back to activate it, it still exists and wasn't destroyed.
             if (collObj != null)
                collObj.SetActive(true);
         });
         coin += 1;
         chubbyScore += 50;
      }
 }

Make a new function in which you enable/disable the game object. Then in the start method repeatedly invoke the recently made function.

//Enables or Disables the GameObject every 5 seconds
void Start()
{
     InvokeRepeating("Repeat", 0, 5);
}

void Repeat()
{
     //Enable or Disable the GameObject
}
Related