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 n-carlson1 · May 15, 2016 at 05:50 PM · scripting problemenumif-statement

Use Enum in an if statement from a different script

I've looked for this answer in quite a few places on this forum, but so far none have proved helpful to get the job done. So my situation is, I have a "Boss" that is controlled by an enum in a script. He has 6 states (in the enum), one of them being Special. These are triggered in the Update() method with a switch statement inside of it. So when the CurrentState is BossActions.Special, it calls Special() in the switch statement which is going to move the boss and then i want him to shoot.

I created another script called BossSpecial where I want to check if the BossAction.Special == true and then call the method if so. Something like

 void Update()
 {
     if(BossScript.BossActions.Special == true )
     {
         Shoot();
     }
 }
 public void Shoot()
 {
   //code here
 }

I know you can't do this this way but I don't know how to accurately compare it. I've tried setting it equal to a variable and then comparing that, but i couldn't figure that out. I tried taking the enum out of the class, but I don't think that's my answer either. I'm just not sure how to check in this instance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eno-Khaon · May 15, 2016 at 06:52 PM

You almost have it. Just make sure that you're calling on the right values.

You have the enumerator, then you have the value from that enumerator:

 // BossScript
 public enum BossActionsEnum { Special, Attack, Etc }
 public BossActionsEnum bossActionsValue;

Then, when you go to use it:

 if(bossScriptInstance.bossActionsValue == BossScript.BossActionsEnum.Special)
 {
     Shoot();
 }

If you're calling them both from a different script (as per my example), you would use an instance of the script, along with its variable for the enumerator. Then, you would compare them with the root script's enumerator options to retrieve the value from it.

Namely, the instances:

 bossScriptInstance.bossActionsValue

and the root script:

 BossScript.BossActionsEnum
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 n-carlson1 · May 15, 2016 at 07:06 PM 0
Share

It never seems to grab the value of the enumeration though. Do I have to set them equal to each other at some point? I did the whole

  public enum BossActions
 {
     Idle,
     $$anonymous$$oving,
     Chasing,
     Patrolling,
     Special,
 }

 public static BossActions bossActionsValue;

I declared it as static so I could reference it in the other script. and then in the if statement I had

 If (BossScript.bossActionsValue == BossScript.BossActions.Special) 
 {
     print("We're shooting);
     Shoot();
 }


But it never seems to fire off

avatar image Eno-Khaon n-carlson1 · May 15, 2016 at 08:53 PM 0
Share

bossActionsValue is a variable, so you do need to give it a value.

If the variable is static, that means you can only have a single state for your boss' activity. Specifically, this means that if you have multiple boss enemies simultaneously, they can only perform the same actions as each other, since they're sharing that global variable.

avatar image n-carlson1 Eno-Khaon · May 15, 2016 at 08:56 PM 0
Share

So then why won't it pass the if test?

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Use a enum from another script 2 Answers

How can i change the camera view when clicking a button ? 1 Answer

Getting an "Enum" value from a Scriptable Object 2 Answers

How to build a humanoid class with body parts that can be accessed? 1 Answer

Can I add an enum value in the inspector? 4 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