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 murkertrer · May 11, 2016 at 06:23 PM · variablesscriptingbasicsscriptingproblemcorrection

variables within variables?

I have been writing some code, but it takes me a lot of time to write every single variable. I was hoping I could make it more efficient by using variables withing variables. Or at least, string or ints within variables to designate the variables that I want to alocate the value of.

So, for example, I have:

 if (Input.GetKeyDown (KeyCode.Alpha2)) {
             key2 = false;
             obj2Type = 0;
             obj2CurAmmo = 0;
             obj2MaxAmmo = 0;
             obj2NextFire = 0;
             this.GetComponent<UiCoordination> ().DestroyAbilityImage (2);
         }
         if (Input.GetKeyDown (KeyCode.Alpha3)) {
             key3 = false;
             obj3Type = 0;
             obj3CurAmmo = 0;
             obj3MaxAmmo = 0;
             obj3NextFire = 0;
             this.GetComponent<UiCoordination> ().DestroyAbilityImage (3);
         }

   


And I was hoping I could write something of the sort:

  if (Input.GetKeyDown (KeyCode.Alpha2)) {
             AssignVariable(2);
             }
             if (Input.GetKeyDown (KeyCode.Alpha3)) {
             AssignVariable(3);
             }
     
     AssignVariable(int it)
     {
                 key[it] = false;
                 obj[it]Type = 0;
                 obj[it]CurAmmo = 0;
                 obj[it]MaxAmmo = 0;
                 obj[it]NextFire = 0;
                 this.GetComponent<UiCoordination> ().DestroyAbilityImage ([it]);
     }








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

2 Replies

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

Answer by Dave-Carlile · May 11, 2016 at 06:49 PM

"Class" and "array" are the words you're looking for.

   class Weapon
   {
     public int Type;
     public int CurAmmo;
     public int MaxAmmo;
     ... etc...
   }

You can then create an array of you weapons...

 Weapon[] weapons = new Weapon[5];

Create your weapons...

 weapons[0] = new Weapon();
 weapons[0].Type = 0;
 weapons[0].CurAmmo = 10;
 ...etc...

And you can access a particular weapon by its index in the array. Reading up on arrays and classes and such will help you get the rest of the way.

Comment
Add comment · 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 YoungDeveloper · May 19, 2016 at 04:43 PM 0
Share

Use [System.Serializable] if you want to see your class in inspector.

    [System.Serializable]
    class Weapon{
      public int Type;
      public int CurAmmo;
      public int $$anonymous$$axAmmo;
      ... etc...
    }

 Weapon[] weapons = new Weapon[5];

avatar image
1

Answer by Flaring-Afro · May 11, 2016 at 06:56 PM

What you are looking for is a Struct. I'm not the best at explaining code but this site is pretty good at breaking down concepts. It's basically a variable of variables and you could make a list of these.

link text

Comment
Add comment · 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 YoungDeveloper · May 19, 2016 at 04:41 PM 0
Share

Struct should be used for very specific things, even more if you can't explain it. It's best to use class if you don't know the difference.

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

46 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

Related Questions

Issue with animation and gravity. 1 Answer

What is this Mode Selection in Editor and how does it work? 0 Answers

Fixing Inverted Camera Controls When Flipped, MouseOrbit 2 Answers

Character Animation Help*! 1 Answer

What's wrong with this simple 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