How Unity displays the UI (panel, button) after a certain period of time

Asked 1 years ago, Updated 1 years ago, 117 views

Please tell me how to display the UI after a certain period of time.
The UI's game object name is EndScene, with three panels and buttons inside (click to move scenes).
I want to hide the beginning, and after 25 seconds, I want to display the whole EndScene so that I can select three buttons, but it doesn't work.
Please advise me on what to do.
At this stage, it looks like the following.
This script is attached to EndScene and the EndScene property of the EndScene component is set to drag and drop EndScene on the game object.

using System.Collections;
using System.Collections.General;
using UnityEngine;
using UnityEngine.UI;

public class EndScene —MonoBehavior
{

    void Start()
    {
        // Call EndScene after 25 seconds
        Invoke("Update", 25f);
    }
    public GameObject endScene;

    void Update()
    {
        endScene.SetActive(true);
    }

}

c# unity3d unity2d

2022-09-30 21:42

2 Answers

Update() is a special function of Unity called every frame automatically, so SetActive will be called immediately.Let's go with another function.

Also, the initial state of the game object should be inactive because it will be displayed after a period of time, but it will not work because it will no longer call any special Unity functions such as Start() or Update() in the inactive.
The workaround is to create an active Empty game object at the top of this game object for display management and apply the script here.


2022-09-30 21:42

Instead of attaching this script to the GameObject, which is called endScene, you should attach it to another always active GameObject (for example, endScene parent

using System.Collections;
using System.Collections.General;
using UnityEngine;
using UnityEngine.UI;

public class EndScene —MonoBehavior
{

    void Start()
    {
        _startTime=Time.realtimeSinceStartup;
    }
    public GameObject endScene;
    private float_startTime;

    void Update()
    {
        if(Time.realtimeSinceStartup-_startTime>=25f)
            endScene.SetActive(true);
    }

}


2022-09-30 21:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.