在Unity中的其他场景中隐藏游戏对象

时间:2020-02-08 19:17:57

标签: c# unity3d

我已使用此代码停止销毁特定的游戏对象。问题是当我将包含此对象的场景更改为另一个不同的场景时,即使在不同的场景中,游戏对象仍然显示... 如何仅在其场景中显示游戏对象。 我希望我明确指出我的问题。

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);
     }
   }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试执行以下操作:

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

}