I’m currently using 2020.1.17 and have been trying to upgrade to 2020.3 (or even 2020.2), but there is a breaking issue in the editor after switching scenes that seems to be a Unity bug. I submitted a report a month ago with no response, so I’m wondering if maybe there is a workaround in the meantime (https://fogbugz.unity3d.com/default.asp?1328780_d677pr8g1ciooq7u).
The basic problem is that calling GameObject.SetActive() in a script stops working after switching scenes, but it seems to only be on UI elements that are in prefabs. I’ve created an extremely simple test case attached below, with the following steps:
- Open the SetActiveTest scene and enter play mode.
- Click anywhere in the game view and the text “Text Prefab” and “Text Object” should appear on screen.
- Exit play mode and switch to the SampleScene scene.
- Switch back to the SetActiveText scene and click anywhere in the game view after entering play mode. This time, only “Text Object” toggles on/off and “Text Prefab” never shows.
Minimal Test Project: Dropbox
The entire script is simply this (yes, I’m aware this is not the most ideal approach performance-wise):
using UnityEngine;
public class SetActiveTest : MonoBehaviour {
void LateUpdate() {
// Toggle the TestText prefab active/inactive when clicking with mouse.
if (Input.GetButtonDown("Fire1")) {
GameObject textPrefab = Find("TestText");
GameObject textObj = Find("TestTextObj");
GameObject sphere = Find("Sphere");
GameObject sphereObj = Find("SphereObj");
textPrefab.SetActive(!textPrefab.activeInHierarchy);
textObj.SetActive(!textObj.activeInHierarchy);
sphere.SetActive(!sphere.activeInHierarchy);
sphereObj.SetActive(!sphereObj.activeInHierarchy);
}
}
// Find game object by name, even if it is inactive.
GameObject Find(string name) {
Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>();
for (int i = 0; i < objs.Length; i++) {
if (objs[i].hideFlags == HideFlags.None) {
if (objs[i].name == name) {
return objs[i].gameObject;
}
}
}
return null;
}
}