• 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
Question by Ercova · Jul 15, 2021 at 12:37 PM · script.enumstateenumerate

Using Enum With Different Scrits

Hello Everyone I want to use Enums. But i want to ask a help about one thing. Think that we have 3 Scripts; One For Enums.

 public enum GameState
 {
     Run,
     None
 };

Second is for time increment and when time is 10 i want Enum equals one of the enum states.

 public class ScriptTwo : MonoBehaviour
 {
     float timer;
     GameState gameState;
     public void Update()
     {
          timer += Time.deltaTime;
         if (timer>=10)
         {
             gameState = GameState.Run;
         }
     }
 }

Third is for using that state for another if condition function in that Script.

 public class ScriptThird : MonoBehaviour
 {   
     GameState gameState;
     public void Update()
     {
         if (gameState==GameState.Run)
         {
             //do Something
         }
     }
 }

How can i make that Scenario? Should Enums be Static or Something?

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Captain_Pineapple · Jul 15, 2021 at 12:59 PM

@pmurph03 is basically right but i'd suggest a different solution:

Something like a gamestate could be handeled with a public static variable.

So you could have a class like this to handle all this:

 public class GameStateHandler{
     public static GameState currentGameState = GameState.None;
 }

Then you can use GameStateHandler.currentGameState to access it from anywhere.

Be caseful at this as static variables are carried over when loading scenes!

Also note that this class is not a Monobehaviour. There is no need to add it as a script somewhere, but it also will not have a Start/Awake/Update function access by itself.

EDIT:

I can only strongly suggest to have a "None" enum value to always be the first value in your enum as this will be the value that a new variable of this type will default to.

Comment

People who like this

0 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 Ercova · Jul 15, 2021 at 02:15 PM 0
Share

Thank you so much it works great.

avatar image

Answer by pmurph03 · Jul 15, 2021 at 12:48 PM

Currently both script two and script third have their own variable of the GameState type. If you want script three to use script twos gamestate, you would need a ScriptTwo variable in ScriptThird. You can set the value of this variable in the inspector, or using GetComponent. Script twos gamestate would also have to be public. Then accessing script twos gamestate in script third would be something like ScriptTwoVariableName.gameState

Comment

People who like this

0 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 Ercova · Jul 15, 2021 at 02:16 PM 0
Share

Thanks for helping ^^

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Enums updating, but then reverting 1 Answer

ScriptableObject Drop-Down Menu with added variables? 1 Answer

Best way to only run only one part of a script at a time? 0 Answers

How to make drag n shooting avaiable only at 0 velocity? 1 Answer

Having an Error with An Enum : error CS0176: Static member `BossOne.BossActionType.Attacking' cannot be accessed with an instance reference, qualify it with a type name instead 2 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