- Home /
How to pass variables from one object in one scene to another object in another scene?
Is it possible to pass variables from one object in one scene to another object in another scene? If so, I need to pass unityNameSelected global variable from first script(which is listed bellow) attached in an object in scene 2 to the second script attached to another object in the scene 1. Thanks for any help.
{
while ( reader.Read() )
{
if ( reader.NodeType == XmlNodeType.Element )
{
if ( reader.HasAttributes )
{
if ( reader.GetAttribute("UnityName") != null )
{
unityName = reader.GetAttribute("UnityName");
if(!values.Contains(unityName))
{
values.Add(unityName);
DontDestroyOnLoad(GameObject.Find("unityName"));
unityNameSelected = unityName;
//string[] unityNameSelected3 = {unityName};
// unityNameSelected =unityNameSelected3;
print(unityNameSelected.ToString());
}
}
}
}
}}
There are 2 methods.
1/ use a singleton with DontDestroyOnLoad
2/ store all your variables to PlayerPrefs before loading the next scene, then read from them and assign values to variables when the new scene loads
could you please give me some examples, links to understand it more clearly?
Sure, am just trying to find my past answer for Singleton. There is alot of information out there for PlayerPrefs in the meantime : http://search.unity3d.com/uss1/?q=PlayerPref&type_of_search=answers
API : http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html
Singleton
Unity Wiki : http://wiki.unity3d.com/index.php?title=Singleton
more info : http://answers.unity3d.com/questions/235417/how-do-i-create-a-static-instance-in-javascript.html
Answer by shaystibelman · Nov 14, 2012 at 08:58 AM
alucardj is right: The right solution (which I use as well because the variables get saved even after you close the app) is PlayerPrefs
My login page code:
function Awake(){
if(!Username && PlayerPrefs.HasKey("Username") && PlayerPrefs.GetInt("Remember")==1){
getUserPrefs();
}
}
function getUserPrefs(){
Username=PlayerPrefs.GetString("Username");
Password=PlayerPrefs.GetString("Password");
}
function saveUserPrefs(){
PlayerPrefs.SetString("Username",Username);
PlayerPrefs.SetString("Password",Password);
PlayerPrefs.SetString("User",Username);
if(createCookie){
PlayerPrefs.SetInt("Remember",1);
}else{
PlayerPrefs.SetInt("Remember",0);
}
PlayerPrefs.Save();
}
No, I'm sorry, I code in JS, but if you click the link and go to each function page, you can choose in which language to display the code.
@Timurov Dude, it's easy ....
Here's some links I found useful in converting between C# / JS :
http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
@shaystibelman nice work, very nice of you to give that script. Future readers will be very happy =]
You're more than welcome! Open sourced code has saved me so much time in the past that I feel it almost necessary to post code. @Timurov, If my answer has helped you, please tag it as an answer to your question, so ppl can see it has been answered.
Yes! I use the Regex.Split() method when I import information from my SQL DataBase:
var sendItemInfo : WWWForm = new WWWForm();
sendItemInfo.AddField("ProductID",ProductID);
var getInfo = new WWW(urlToDatabase,sendItemInfo);
yield getInfo; var received_data = Regex.Split(getInfo.text,"");
//divide split data into vars
productName = received_data[0];
productText = received_data[1];
productPrice = received_data[2];
productWebPage = received_data[3];
Answer by Timurov · Nov 14, 2012 at 09:43 AM
very easy to use for single value :), actually I'm going to pass array values which collects at least 5 string values. Is there a way to pass them through PlayerPrefs?
please signal my answer as a correct answer for this question, as for the array question you have opened another thread.
I've added a tutorial on Unity Gems showing how to store complex things in PlayerPrefs like arrays and lists...
Answer by whydoidoit · Nov 14, 2012 at 10:40 AM
You can store anything in player prefs if you use the BinaryFormatter.
var m = new MemoryStream();
var b = new BinaryFormatter();
b.Serialize(m, anyObjectYouLike);
PlayerPrefs.SetString("ClassData", Convert.ToBase64String(b.GetBuffer()));
Like this to decompress it:
var m = new MemoryStream(Convert.FromBase64String(PlayerPrefs.GetString("ClassData"));
var b = new BinaryFormatter();
var o = (AnyObjectYouLike)b.Deserialize(m);
You'll need System, System.IO and System.Runtime.Serialization.Binary I think. The class has to have a parameterless constructor.
And it can't be a $$anonymous$$onoBehaviour. You can use Unity Serializer to save $$anonymous$$onoBehaviours to strings - but it's pretty techy and probably not what you need to do anyway.
Your answer
Follow this Question
Related Questions
How to access variables from another script on collision ? 1 Answer
Setting Player Instance for Scripts doesn't work. 4 Answers
Creating Dropdown menue for variables in the Inspector using C# 1 Answer
Trying to access a variable from another script attached to same object. 1 Answer
Distribute terrain in zones 3 Answers