首页 > 解决方案 > 在 Unity 中隐藏其他场景中的游戏对象

问题描述

我已经使用此代码来停止破坏特定的游戏对象。问题是当我将包含此对象的场景更改为另一个不同的场景时,即使在不同的场景中游戏对象仍然显示...如何仅在其场景中显示游戏对象。我希望我清楚地说明我的问题。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DontDestroy : MonoBehaviour {
 void Awake() {
     DontDestroyOnLoad(this.gameObject);
     if(FindObjectsOfType(GetType()).Length > 1) {
         Destroy(gameObject);
     }
   }
}

标签: c#unity3d

解决方案


你可以尝试做这样的事情:

m_Scene = SceneManager.GetActiveScene();
sceneName = m_Scene.name;
//Do logic by checking the specific scene you want
if (sceneName == YOUR_SCENE) {
    this.gameObject.SetActive(false);  //Set false to hide, true to show

}

推荐阅读