What I’m trying to do is have a laser that after 3 seconds there is no laser but then after 3 more seconds, after there is no laser, the laser is back. Does anyone know how to do this?
Hey,
If the laser is a game object, you can use this code to toggle it on/off every 3 seconds:
// Drag your laser game object into this field in the inspector.
[SerializeField] GameObject laser;
// This is the timer we use to count up to 3 seconds.
float timer = 0f;
void Update()
{
// Here, we want to add 1 to the timer every second. To do that, we need to multiply 1 by Time.deltaTime.
timer += 1f * Time.deltaTime;
// This if statement is checking when the timer reaches 3 seconds.
if (timer >= 3f)
{
// This resets the timer back to 0.
timer = 0f;
// activeInHierarchy checks whether or not the lase is active. We add ! before to get the opposite. So we set the laser to the opposite of its current state.
laser.SetActive(!laser.activeInHierarchy);
}
}
I’ve added a bunch of comments explaining what each part of the code does. Make sure that you put the script on an object other than the laser (it can be a parent).