到目前爲止,我已經嘗試過。我想要在場景中傳遞變量的值。我如何保持價值。我想從一個場景獲取遊戲對象的位置,但腳本重新初始化了GameObject的值。在場景中傳遞並保持變量值
using UnityEngine;
using System.Collections;
public class Maintainer : MonoBehaviour {
public GameObject v;//I want to maintain this value across the scene
public GameObject cameraGO;
static Maintainer instance;
// Use this for initialization
void Awake() {
}
void Start() {
Debug.Log("awake ");
if (instance != null)
{
Debug.Log("Instance not null");
//Dont want new
Destroy(gameObject);
}
else
{
Debug.Log("instance is null");
DontDestroyOnLoad(gameObject);
instance = this;
}
//DontDestroyOnLoad(gameObject.transform);
}
// Update is called once per frame
void Update() {
if (cameraGO != null)
{
v = cameraGO;
Debug.Log("camera position " + cameraGO);
}
else {
Debug.LogError("Camera not found!");
}
Debug.Log("Camera Position : "+ v);
}
void OnGUI() {
if (GUI.Button(new Rect(10, 10, 150, 100), "Load Scene 2")){
print("You clicked the button!");
Application.LoadLevel ("Scene2PassValue");
}
if (GUI.Button(new Rect(10, 150, 150, 100), "Load Scene 1"))
{
// Debug.Log("Camera Position : " + v.transform.position);
print("You clicked the button!");
Application.LoadLevel("Scene1PassValue");
}
}
}
您是否嘗試將該值設爲靜態類字段? –
你能更具體嗎?你想要傳遞哪種價值,是你需要在另一個課程中引用的價值? – Takarii
@Takarii gameobject v值 –