Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Owenja147 · Jul 15, 2016 at 12:13 PM · c#scripting problemscenevariables

How do I access a variable from a different scene using C#?

So far I have two scenes in my game:

  1. the main menu scene

  2. the actual game scene

In the main menu screen I have a button which enables a type of game mode. If enabled, the player won't be able to save the game (by disabling the save game script).

I want to know how do I access a Boolean in a script in the Main Menu scene from a script in my Game scene so that I can disable my save game script (C# only).

For example: In Main Menu scene I would have:

 public static bool hardcoreMode;
 
 void Awake ()
 {
            //some code..
 
            hardcoreMode = false;
 
            //more code..
 }
 
 void OnGUI()
 {
             //some code...
     
             if(GUILayout.Button("HardCore Mode"))
                     hardcoreMode = true;
 
              //more code...
 }

So how do I access the Boolean "hardcoreMode" from a script in my Game scene?

(Beginner in Unity and scripting in general)

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
0
Best Answer

Answer by Crashjumper · Jul 15, 2016 at 01:46 PM

I believe you could make you boolean a static variable by adding static in front of it, or have a manager game object with that variable and dontdestroyonload.

Dontdestroyonload https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

Static variable https://unity3d.com/learn/tutorials/topics/scripting/statics

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 Owenja147 · Jul 15, 2016 at 02:39 PM 0
Share

I know how to use static variables since it's common for me to reference other scripts, but I only ever done that with variables and classes in the same scene. Will the value stay the same if I reference a variable from a different scene? like in my example code in the question.

If I use DontDestroyOnLoad on an empty game object with my script attached to it, will that be an efficient way to achieve what I want? Or will it be better if I just go back to static variables if that works across different scenes like I have asked about before?

avatar image Crashjumper Owenja147 · Jul 15, 2016 at 02:51 PM 1
Share

Yes the static variable will exist between scenes.

See this previous question. http://answers.unity3d.com/questions/41891/static-variables-between-scenes.html

avatar image PavanHolkar · Jul 16, 2016 at 07:47 AM 0
Share

$$anonymous$$ake sure you use limited static variables else you may get performance issue.

and make sure you use following code in ur script

 public static "YourScriptName" Instance;
     void Awake()
     {    
         
         if (Instance == null)
             Instance = this;
         else if (Instance != this) 
         {            
             Destroy(gameObject);
             return;
         }
         DontDestroyOnLoad(this);
     }

Or else you will end up will multiple game object when you reload the scenes

avatar image
0

Answer by PavanHolkar · Jul 15, 2016 at 12:16 PM

Use Singletone class

Comment
Add comment · Show 4 · 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 Owenja147 · Jul 15, 2016 at 12:49 PM 0
Share

Would you make an example, please? Like I've mentioned, I am new to Unity and Scripting in general.

avatar image kubawich Owenja147 · Jul 15, 2016 at 12:56 PM 0
Share

Actually singletone isn't unity stuff, but normal program$$anonymous$$g pattern :)

avatar image PavanHolkar · Jul 15, 2016 at 02:03 PM 0
Share

you can find more about singletons in following link :

http://wiki.unity3d.com/index.php/Singleton

avatar image Owenja147 · Jul 15, 2016 at 02:31 PM 0
Share

For a beginner like me, it's a bit confusing to use Singletons but thank you for your input. I just need to take a bit of time to understand it.

avatar image
0

Answer by jorrit5477 · Jul 15, 2016 at 03:33 PM

This seems like a setting you want to store throughout the game, including multiple play sessions, for which you can use PlayerPrefs. This is common practice for storing player/game data.

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

8 People are following this question.

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

Related Questions

Access loading screen scene components from Main Menu? 1 Answer

Find GameObject in another loaded scene 2 Answers

Changing class data with a reference? 2 Answers

Referencing a non static variable from another script C# Unity 1 Answer

Are C# Global Variables Safe In Unity? 5 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