Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Disable a timeline after it has finished?

Discussion in 'Scripting' started by Psylova, Dec 9, 2019.

  1. Psylova

    Psylova

    Joined:
    May 5, 2016
    Posts:
    7
    Hey everyone.

    At the beginning of my game, I have the player walk over a trigger which enables a timeline, playing an animation and sounds in a specific order. I intend to use this a lot, but I'm not sure if having lots of timelines enabled would cause issues in the game.

    So I tried setting up a script to disable the timeline after a certain amount of seconds, and allow me to change the amount of seconds in the inspector on every individual timeline. It sits on the timeline GameObject:

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

    public class DisableInSeconds : MonoBehaviour
    {
    public int inSeconds;
    public GameObject thisTimeline;

    void OnEnabled()
    {
    StartCoroutine(DisableTimeline());
    }

    IEnumerator DisableTimeline()
    {
    while (true)
    {
    yield return new WaitForSeconds(inSeconds);
    thisTimeline.SetActive(false);
    }
    }
    }

    I tried using this.GameObject.SetActive(false); instead of having to manually set the timeline in the inspector, but it didn't work. The while (true) thing was also an attempt to get it to work, and I don't think it's necessary.

    Unfortunately it doesn't actually disable anything. There is no error in the console, it just doesn't do anything. I tried making the timeline itself disable the object after it had finished by getting the object to be active until the end of the timeline, but it didn't do anything either. Could anyone give me any advice?

    Thanks!
     
    Last edited: Dec 10, 2019