'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.
Solution 1:[1]
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);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yuris |



