Something along the lines of this will do:
public GameObject doorOne;
public float deactivateTime = 3.0f;
void OnMouseUp()
{
StartCoroutine(TimedDeactivate(doorOne, deactivateTime));
}
private IEnumerator TimedDeactivate(GameObject objectToDeactivate, float time)
{
objectToDeactivate.SetActive(false);
yield return new WaitForSeconds(time);
objectToDeactivate.SetActive(true);
}
Alternatively you can make a new script you can add to object, to make them objects that support timed deactivation. Then you'd have to access the object using that type (either by casting or by having the reference with that type) so you can call that method.
public class TimedDeactivatedObject : MonoBehaviour
{
public float DeactivateTime = 3.0f;
public void DeactivateForTime()
{
StartCoroutine(TimedDeactivate(this.gameObject, DeactivateTime);
}
private IEnumerator TimedDeactivate(GameObject objectToDeactivate, float time)
{
objectToDeactivate.SetActive(false);
yield return new WaitForSeconds(time);
objectToDeactivate.SetActive(true);
}
}
Then just call:
public TimeDeactivatedObject door;
void OnMouseUp()
{
door.DeactivateTime = 1.5f //or set in editor
door.DeactivateForTime();
}
↧