- Home /
Bool activate/de-activate on timer C#
Hello there. I have a script here that I don't know how to get working anymore. I've had to go back to this numerous times and completely lost with what I was doing. (Still very new to coding).
Here's my script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoolTRUEOnTimer : MonoBehaviour
{
public GameObject objectToActivate;
public int TimeBeforeActivate;
public int TimeBeforeDisable;
private void Start()
{
StartCoroutine(ActivationRoutine());
}
public IEnumerator ActivationRoutine()
{
//Wait for 14 secs.
yield return new WaitForSeconds(8);
//Turn My game object that is set to false(off) to True(on).
objectToActivate.SetActive(true);
//Turn the Game Oject back off after 1 sec.
yield return new WaitForSeconds(15);
//Game object will turn off
objectToActivate.SetActive(false);
}
}
So for the time before activate and time before disable I need it to be public so i can use this on different objects but if you can link them to of course objectToActivate it would be most helpful. Also I forgot to mention, could you get it to disable the gameobject when you play then have it enable on the desired time?
This code would work, what are you wanting to do with it? You are referencing a gameObject with objectToActivate so you can disable and enable it at will.
Yes. I need the functions to be public. So I can use the script for more than one thing.
I'm trying to port a game. Halo. Halo combat evolved anniversary to android, i'm also trying to do one of the missions but I can't without having this script ;'(
In your start function you can just do : objectToActivate.SetActive(false) This will make sure the object is deactivated when the game starts.
In your co-routine if you just need to flash the object
public IEnumerator ActivationRoutine()
{
//Wait for 14 secs.
yield return new WaitForSeconds(14);
//Turn $$anonymous$$y game object that is set to false(off) to True(on).
objectToActivate.SetActive(true);
//Turn the Game Oject back off after 1 sec.
yield return new WaitForSeconds(1);
//Game object will turn off
objectToActivate.SetActive(false);
}
Thankyou very very much!!! It works now thankyou very much friend! Thankyou so much for getting it to successfully work!!!!
Answer by naiemali1171987 · May 20, 2019 at 07:34 PM
For anyone who wants to know how to use the script, attach it to an empty gameobject, then in the inspector drag and drop what gameobject you want to enable after a certain time and it should work. Don't attach the script to the object your going to enable because when you start the game that gameobject will disable and enable itself after the time wanted. But the script won't function then because it's on the gameobject that was disabled.
Your answer
Follow this Question
Related Questions
Help With Disabling/Enabling single GameObject 1 Answer
Enable/Disable Game Object With GUI Button 1 Answer
How to disable an array of projectors 2 Answers
how to perform undo action in unity? 2 Answers