До сих пор я пробовал это. Я хочу передать значение переменной по всей сцене. Как сохранить значение. Я хочу получить позицию игрового объекта из одной сцены в другую, но сценарий повторно инициализировал значение переменной GameObject v.Передача и сохранение значения переменной по сцене
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 value –