- Home /
How can i pass a variable from a scene to another?
Hi, I need to pass a variable part of this script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class nameSelection : MonoBehaviour {
public Text namePosition;
public string nomep;
public Text presentation;
Button bottone;
bool nosel=false;
public GameObject arrow;
void OnMouseDown(){
if (namePosition.text != "" && nosel==false) {
nomep = namePosition.text;
PlayerPrefs.SetString("nomePersonaggio",nomep);
presentation.text += " So, you are "+name+"? Nice name!";
presentation.text += " Let's select your first monster!";
nosel=true;
arrow.SetActive(true);
}
}
}
Her name is "nomep" and I need her in another scene. How I can pass her value?
(Sorry for my bad english)
Answer by Ferb · Jun 22, 2015 at 03:15 PM
There are different ways, but I often have a script containing all the variables I want to share between scenes, all defined as static.
using UnityEngine;
using System.Collections;
public class SharedVariables{
public static string nomep;
}
Now you can call or set this from any script in any scene, with a line like
SharedVariables.nomep = "Bob";
Answer by Graham-Dunnett · Jun 22, 2015 at 03:22 PM
Try using a ScriptableObject:
http://docs.unity3d.com/ScriptReference/ScriptableObject.html
A ScriptableObject doesn't get deleted when a scene changes.
Your answer
Follow this Question
Related Questions
Keep GameObject to variable linkage on LoadLevel 0 Answers
How to "Lock" a variable for X seconds 1 Answer
Playerpref float is out of range? 1 Answer
Changing character run speed based on another scripts variable 2 Answers
Scene as a variable 6 Answers