简体   繁体   中英

Unity C# - Hide / Unhide a Button in a different scene

I'm exploring a bit in Unity and C#, I searched everywhere for a solution without luck.

Problem:

  • In Scene A, I have an object button called "Button_2" -> it's hidden.
  • In Scene B, I have another button called "Button_1", and this one, must unhide "Button_2", in the other scene "Scene A"

How can I achieve this, if possible of course?

Thank you.

EDIT 1:
I tried the following:

  • Used DontDestroyOnLoad. Basically can't use it on a single button. Unity doesnt allow it. So, I had to do it on the canvas, but when I switched to another scene, was a huge mess with both scenes mixed.
  • Tried Yuris solution and suggestions and didn't work. Scenes just don't interact with each other at all.
  • Tried to combine both of above with the prefab concept Yuris suggested aswell and still no luck.

I will keep doing my best, but my knowledge is very limited. I'll be back with more feedback if I make progress.

EDIT 2:

  • Made many experiments with a non monobehaviour script file and class with only variables to be accessed by a script file that is linked to gameobjects in the diferent scenes. I wasnt hable to access the variables on the non monobehaviour file. Looks like unity doesn't like non monobehaviour and didn't work.

You can make the button a prefab and assign it on a script in the Scene B, and then when you load scene A again, it will be active.

Remember that if you use that prefab anywhere else, it will be active there too.

Example:

项目 督察 等级制度

Example.cs:

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

public class Example : MonoBehaviour
{
    public GameObject button;

    // Start is called before the first frame update
    void Awake()
    {
        button.SetActive(true);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM