Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Timurov · Nov 14, 2012 at 08:31 AM · c#objectvariables

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());
                                          
                 }
                             }
                             }
                     }
                 }}
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Nov 14, 2012 at 08:43 AM 1
Share

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

avatar image Timurov · Nov 14, 2012 at 08:44 AM 0
Share

could you please give me some examples, links to understand it more clearly?

avatar image AlucardJay · Nov 14, 2012 at 08:57 AM 1
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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();
 }

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Timurov · Nov 14, 2012 at 09:04 AM 0
Share

do you have it in c#?

avatar image shaystibelman · Nov 14, 2012 at 09:07 AM 0
Share

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.

avatar image AlucardJay · Nov 14, 2012 at 09:08 AM 2
Share

@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

http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

@shaystibelman nice work, very nice of you to give that script. Future readers will be very happy =]

avatar image shaystibelman · Nov 14, 2012 at 09:25 AM 1
Share

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.

avatar image shaystibelman · Nov 14, 2012 at 10:24 AM 1
Share

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];
 
Show more comments
avatar image
0

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shaystibelman · Nov 14, 2012 at 03:42 PM 2
Share

please signal my answer as a correct answer for this question, as for the array question you have opened another thread.

avatar image whydoidoit · Nov 14, 2012 at 09:14 PM 0
Share

I've added a tutorial on Unity Gems showing how to store complex things in PlayerPrefs like arrays and lists...

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Nov 14, 2012 at 10:44 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges