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
1
Question by Fung · Apr 10, 2013 at 09:42 AM · scenevariable

how to get other script variable from other scene?

how to get other script variable from other scene?

Now, I have two java scripts from different scene(login.js, login scene and upload.js, upload scene )

I hope can get a variable from login scene in upload scene.

Used for android.

I'm Beginner, please give me a example code. My English is very pool, hope we can know.

thank you!

Comment
Add comment
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

3 Replies

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

Answer by prototype7 · Apr 10, 2013 at 10:41 AM

Create singleton class, store your variable into it, access from other script in any scene.

login.js

 class Login
  {
  
     // This is the singleton instance that is shared with everyone.
     private static var Instance : Login;
  
     // Everything that needs to get the singleton
     // instance, calls this function to get it.
     public static function GetInstance() : Login
     {
         if (Instance == null)
             // This is the first time the function was called.  Need to
             // construct the shared instance.
             Instance = new Login();
         return Instance;
     }
  
      var state : boolean ;
  
      // Private constructor.  This is what forces the class
      // to be a singleton since the only way to construct it
      // is via GetInstance().
      // NOTE:  Private constructors are not normally allowed
      // in JavaScript.  But it is OK in Unity.
      private function Login()
      {
         state=false;
      }
  
      function GetState() : boolean
      {
        return state;
      }
  
 }

upload.js

 private var control : Login;
  
 function Start() 
 {
     control = Login.GetInstance();
     Debug.Log(control.GetState());
     // The next line would cause a compile time error due to
     // the constructor's protection level.
     // control = new Login();
 }
Comment
Add comment · Show 3 · 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 Fung · Apr 10, 2013 at 04:14 PM 0
Share

Test O$$anonymous$$!! thank you!

avatar image prototype7 · Apr 11, 2013 at 12:56 AM 1
Share

Hi Fung if Test Ok, you should accept one of the answer to become green so others can easy to read. it's a checklist mark on the left

avatar image Fung · Apr 11, 2013 at 09:33 AM 0
Share

O$$anonymous$$, I'm forgot. Sorry!

avatar image
1

Answer by svenskefan · Apr 10, 2013 at 12:23 PM

Hi! The easiest way would be to create a static variable somewhere. You might even create a static class to hold your persistent data.

Comment
Add comment · Show 3 · 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 aldonaletto · Apr 10, 2013 at 12:53 PM 1
Share

Yes, static variables are created when the game starts, and survive across scenes until the game finishes. But be aware that static variables are unique: if you have an enemyHealth static variable and multiple enemies in scene, for instance, when you kill one enemy all of them will fall dead because the health variable is the same for all instances.

You can declare a static variable this way:

 static var playerHealth: int = 100;

The variable playerHealth is created with value 100 when the game starts, and keeps its current value even when new scenes are loaded (notice that the initialization to 100 is done only once, at program start)

avatar image fafase · Apr 10, 2013 at 12:56 PM 0
Share

Also, a static variable remains for the rest of the program. $$anonymous$$eaning if you need to pass a particular value from scene 1 to scene 2 but not anymore for the rest of the 20 other levels, your variable is there but will not be used ever again. Note that one integer will not get you running out of memory...

avatar image Fung · Apr 10, 2013 at 04:18 PM 0
Share

Thank you! Solve my problem!!

avatar image
0

Answer by dreammakersgroupAdmin · Apr 10, 2013 at 10:53 AM

you have to save that variable to playersPref then take it again in other scene

Comment
Add comment · 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

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

15 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 avatar image avatar image avatar image

Related Questions

Setting variable as type "Scene" 4 Answers

How to make two scripts share same variable? 1 Answer

Distance Variable Won't Change. What's Wrong With My Script? 1 Answer

Javascript 1 Answer

Accessing Script From Other Script Causes Lag? 1 Answer


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