4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

unity > シーン間移動でDontDestroyOnLoad(this)したGameObjectの関連付けが消える

Last updated at Posted at 2015-09-02
動作確認
Unity 5.1.2-f1 on MacOS X 10.8.5
  • Mainシーン
    • UI > Button
    • UI > InputField
    • UI > Toggle
    • GameObject: someProcess
  • Otherシーン
    • UI > Button  

MainシーンからButtonでOtherシーンへ移動し、OtherシーンからButtonでMainシーンに戻るようにしている。

Main -> Other -> Main にシーン移動した時、someProcesゲームオブジェクトのスクリプト(someProcessScript.cs)にインスペクタで関連づけていたInput Field, Toggleが両方ともNoneになってしまう。

someProcessScript.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class someProcessScript : MonoBehaviour {

	public InputField IF_name;
	public Toggle Toggle_test;
	private static bool created = false;

	void Awake() {
		if (!created) {
			created = true;
			DontDestroyOnLoad (this.gameObject);
		} else {
			Destroy (this.gameObject);
		}
	}

	void Start () {
	}
	
	void Update () {
	
	}
}

どうもMainシーンに戻った時にDestory()されているのは、新しく作られた方でなく、もとからあったほうで、そのため関連付けがNoneになっているようだ。

新しく作られた方をDestroy()する方法を模索中。

4
5
2

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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?