Respawning item

Hi, I was wondering how I would make an item active again after 5 seconds. For example, a character “picks” up an item. When he does this it makes that item go inactive. How do I make this active again after 5 seconds? I don’t know if this helps but I used this to make the object go inactive other.gameObject.setActive(false), and I need to make that true after 5 seconds.

Here is one way: You create a respawn timer script on a different GameObject than the item. This way the timer can run while the item is inactive and activate it after x seconds.

using UnityEngine;

public class SetActiveAfterTime : MonoBehaviour
{
	[Tooltip("The GameObject to set active.")]
	public GameObject targetObject;

	[Tooltip("The duration in seconds.")]
	public float timerInterval = 5f;

	private float timer;

	private void Start()
	{
		this.enabled = false;
	}

	public void StartTimer()
	{
		timer = timerInterval;
		this.enabled = true;
	}

	private void Update()
	{
		if (timer <= 0f)
		{
			targetObject.SetActive(true);
			this.enabled = false;
		}
		timer -= Time.deltaTime;
	}
}

You item code can communicate with the timer and request it being activated again like this:

using UnityEngine;

public class Item : MonoBehaviour
{
	public SetActiveAfterTime respawnTimer;

	private void OnTriggerEnter(Collider other)
	{
		// Or however this item is picked up.
		if (other.CompareTag("Player"))
		{
			// Despawn this item object.
			this.gameObject.SetActive(false);

			// Tell the respawn timer that it should reactive this item object.
			// Alternatively, pass as parameter to StartTimer method.
			respawnTimer.targetObject = this.gameObject;
			respawnTimer.StartTimer();
		}
	}
}

This is just one of many ways but should work nice and simple. You set the references between item and timer via the inspector.

Also, see the Unity tutorial about collecting pick-up objects.