tukapai
1/9/2014 - 11:52 AM

【Unity】シーンを跨いでもスコアなどのデータを保持することができるスクリプト。 参考URL : http://goodscientist.tumblr.com/post/25617673512/dontdestroyonload

【Unity】シーンを跨いでもスコアなどのデータを保持することができるスクリプト。 参考URL : http://goodscientist.tumblr.com/post/25617673512/dontdestroyonload

#pragma strict

// スコア
var score : int = 0;

// 生成しているかどうか
private static var created:boolean = false;

function Awake(){
	// シーンを切り替えても指定のオブジェクトを破棄せずに残す
	if(!created){
		DontDestroyOnLoad(this.gameObject);
		created = true;
	} else {
		Destroy(this.gameObject);
	}
}

function Start () {

}

function Update () {

}

function OnGUI(){
	GUI.Label(Rect(10,20,120,20),"SCORE : " + score);
}
using UnityEngine;
using System.Collections;

public class GameDataStorageScript : MonoBehaviour {
	private static bool created = false;
	int score = 0;

	void Awake(){
		if(!created){
			// シーンを切り替えても指定のオブジェクトを破棄せずに残す
		    	DontDestroyOnLoad(this.gameObject);
			// 生成した
			created = true;
		} else {
			Destroy(this.gameObject);
		}
	}
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnGUI(){
		GUI.Label(new Rect(10,20,120,20),"SCORE : " + score);
	}
}