LoginSignup
2
0

More than 5 years have passed since last update.

[Unity] DontDestroyOnLoad()を解除する

Last updated at Posted at 2018-12-14

一度 DontDestroyOnLoad() したオブジェクトを再びLoadScene()で削除されるようにするには

SceneManager.MoveGameObjectToScene(gameObject, SceneManager.GetActiveScene());

とすればよい。

具体的には、

Sphereを置いたSceneAと
2018-12-14_121905.png
Cubeを置いたSceneBを作り、
2018-12-14_121946.png
それぞれのSphere,Cubeに以下のスクリプトを張り付ける

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

public class LoadScene : MonoBehaviour {
    [SerializeField] string m_nextScene;
    void Start () {
        DontDestroyOnLoad(gameObject);
    }

    void Update () {
    }

    private void OnMouseDown()
    {
        SceneManager.MoveGameObjectToScene(gameObject, SceneManager.GetActiveScene());
        SceneManager.LoadScene(m_nextScene);
    }
}

SceneAのSphereにはNextSceneに SceneBを、
2018-12-14_122313.png

SceneBのCubeにはNextSceneに SceneAをいれる
2018-12-14_122611.png

これで、シーンロード直後はDontDestroyOnLoad シーンに入っているSphere,Cubeが
2018-12-14_123043.png

LoadScene()で消えるようになる
2018-12-14_123131.png

参考文献

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0