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 Cryno1000 · Sep 09, 2020 at 03:46 AM · scripting problemvariablesvariable-definition

Best way to assign a bunch of variables?

I'm making a game with a lot of abilities and the player can assign each one to a direction (up, down, left right). So in my abilities script I have variables for each ability's cool down time and effect. So instead of just assigning each of these manually like this:

 if (equippedTopAbility == "lightning")
         {
             topAbilityCooldownTime = lightningCooldownTime;
             topReadyEffect = lightningReadyEffect;
         }
 if (equippedRightAbility == "lightning")
         {
             rightAbilityCooldownTime = lightningCooldownTime;
             rightReadyEffect = lightningReadyEffect;
         }
 if (equippedBottomAbility == "lightning")
         {
             bottomAbilityCooldownTime = lightningCooldownTime;
             bottomReadyEffect = lightningReadyEffect;
         }
  if (equippedLeftAbility == "lightning")
         {
             leftAbilityCooldownTime = lightningCooldownTime;
             leftReadyEffect = lightningReadyEffect;
         }

I would like to create a function with variables that I determine earlier. So for example the function looks like: public void AssignVariables(string direction, float CooldownTime, GameObject ReadyEffect) and I want to assign topAbilityCooldownTime and topReadyEffect.

So my question is whether or not there is a way to assign variables like the following

 direction + "AbilityCooldownTime" = CooldownTime;
 direction + "ReadyEffect" = ReadyEffect;

I know that this doesn't work but was wondering if there was an easy way to do this. (The variables I want to assign are topAbilityCoolDownTime and topReadyEffect)

Edit: Thanks for the help everybody! I got it all figured out!

Comment
Add comment · Show 2
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 $$anonymous$$ · Sep 09, 2020 at 04:49 AM 0
Share

use enum it has more states than bool.

avatar image BenWiller1989 · Sep 09, 2020 at 06:46 AM 0
Share

You could use structs in combination with the variables you need. For example enum, float, int, bool, Gameobjects, Vector3 etc. That way, it's easier to sort variables for specific needs.

 public struct myStruct {
 float myFloat;
 Int myInt;
 myEnum;
 bool myBool;
 } ;
 
 enum myEnum {
 first Name, second Name.... million Name etc
 }
 
 myStruct = new myStruct() ;

2 Replies

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

Answer by Roger_0123 · Sep 11, 2020 at 07:52 AM

Hi @Cryno1000, you may use a C# Dictionary:

 public Dictionary<string, float> cooldownTimes = new Dictionary<string, float>();
 
 public Dictionary<string, GameObject> readyEffects = new Dictionary<string, GameObject>();
 
 
 //....
 
 
 public void AssignVariables(string direction, float cooldownTime, GameObject readyEffect){
 
    this.cooldownTimes[direction + "AbilityCooldownTime"] = cooldownTime;
 
    this.readyEffects[direction + "ReadyEffect"] = readyEffect;
 
 }

Check the C# Documentation for info on how to use Dictionaries

Hope this helps! (PS: sorry for code formatting)

Comment
Add comment · Show 2 · 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 ShadyProductions · Sep 11, 2020 at 10:38 AM 0
Share

I fixed the formatting for you, it's not that difficult.

https://answers.unity.com/page/userguide.html?_ga=2.209178842.959273939.1598783200-1843278623.1531167036

Scroll to the "Posting Questions, Answers and Comments" code formatting section.

avatar image Roger_0123 ShadyProductions · Oct 27, 2020 at 09:19 AM 0
Share

Thank you!

avatar image
0

Answer by ShadyProductions · Sep 11, 2020 at 10:49 AM

Like few have mentioned, you could use enums and dictionaries to do it.

I suggest keeping your ability stuff in a nice seperate class.

 public enum Direction
 {
     Left,
     Right,
     Up,
     Down
 }
 
 public class Abilities
 {
     public readonly Dictionary<Direction, float> Cooldowns;

     public Abilities()
     {
         Cooldowns = new Dictionary<Direction, float>();
         foreach (var direction in (Direction[])Enum.GetValues(typeof(Direction)))
             Cooldowns.Add(direction, 0f);
     }
 }

When defined you can do something like

 Abilities abilities = new Abilities();
 abilities.Cooldowns[Direction.Left] = 5f;
 var cooldown = abilities.Cooldowns[Direction.Left];
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

244 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 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

I'm unable to clear a variable that is used in multiple scripts. 0 Answers

Statics variables in structs for jobs system 0 Answers

Float randomly being set to 0 spontaneously 1 Answer

AudioClip stuttering while loop is OFF and not using function Update? 1 Answer

How to READ a variable VALUE from other Object Script? 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