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 Firedan1176 · Nov 02, 2014 at 08:38 PM · variablefunctionvariablesclasscustom

Magic Spell Types - SpellType.Fire/Water/Earth?

I literally don't know what I'm looking for and someone may have already answered this. But I want to make my own function:

 public void UseMagicSpell() {}

and I want to use my own special form of variables instead of a string so, instead of:

 public void UseMagicSpell(string spell) {}

OR

 UseMagicSpell("FireSpell");

I want to use:

 public void UseMagicSpell(SpellType spellType) {}

OR

 UseMagicSpell(SpellType.Fire);

I'm not sure that's entirely what I'm wanting but I want to use this UseMagicSpell function so I don't have to do it line by line later on (whatever's in this UseMagicSpell function).

I hope you know what I'm talking about. I know I need a class though, like:

 public class SpellType {}

but I don't know what I put inside. If I want SpellType.Fire, SpellType.Water, SpellType.Earth etc. what do I put inside of this class so I can do so later on?

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

Answer by Kiwasi · Nov 02, 2014 at 09:01 PM

You want an enum

 public enum SpellType {Fire, Water, Air, Pokemon};
 public SpellType mySpellType = SpellType.Water;

 public void Cast (SpellType spellType){
     if(spellType == SpellType.Water){
         Debug.Log ("Splash");
     }
 }


Comment
Add comment · Show 6 · 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 Firedan1176 · Nov 02, 2014 at 09:04 PM 0
Share

Thank you! What does line 2 do?

avatar image Kiwasi · Nov 02, 2014 at 09:26 PM 0
Share

Line two was just an example of how your could declare a variable of the SpellType. It actually does nothing useful. If you throw it into a script you will get the warning "Variable mySpellType is assigned its value but not used" or something similar.

avatar image kburkhart84 · Nov 04, 2014 at 01:57 AM 2
Share

Just for reference, an enum is basically an int, but with .value ins$$anonymous$$d of 1,2,3. So SpellType.Water would be 1, and so on. But since the whole point of the enum is to make things readable and consistent, so you wouldn't want to use it with direct values.

As you see in the code above, it says if spellType == SpellType.Water....well you could also use a switch:case set in the same manner, considering you may have a much longer list of spell types.

avatar image Kiwasi · Nov 04, 2014 at 02:05 AM 0
Share

@kburkhart84 enums are ints by default, but they can also be defined as other types if it makes sense.

The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.

avatar image kburkhart84 · Nov 04, 2014 at 04:03 AM 0
Share

@Bored$$anonymous$$ormon

That may be true, but I personally just use enums for organization and readable code, not for much else, so the only thing it makes sense to me for is as the the default ints. I can believe that other uses arise by using other data types, but I haven't found it yet :)

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

27 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

Related Questions

Can I choose what to pass into .getcomponent<{VARIABLE}>(); 1 Answer

Calling a Function from other script ( C# ) 1 Answer

object assigns but does not show when added to list, or any other var 0 Answers

Custom Documentation for Monodevelop 1 Answer

Need help with this please. 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