• 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 /
  • Help Room /
avatar image
0
Question by Caiovvs · Sep 30, 2015 at 12:31 AM · variablescriptingbasicsorder

Assigning variables depending on other variables

I know the title is a little vague but I couldn't think of anything better, sorry!

I'll try to explain: Let's say I pick 1 "Unit" out of a maximum of 6, and I want to change it's "Strength" variable, which is kept in another script which keeps track of every 6 units "Strength" (as "unit1strength, unit2strength", for example).

The units are part of a List of units, going from 0 to 5 (6 total). What I want to do is assign it's "Strength" as following:

 if(unit == unitsList[4])
 {
     unit.GetComponent<Unit>().strength = anotherscript.unit5strength;
 }
 if(unit == unitsList[5])
 {
     unit.GetComponent<Unit>().strength = anotherscript.unit6strength;
 }

However, I feel like the code becomes really redundant and there's a better way to put it without copypasting the whole thing and changing just the numbers, but I'm relatively new at coding and can't figure out how. If I did it the way I showed above, there would be an enormous chunk of unnecessary code, since in the game, "Strength" is only one of the many attributes the "Unit" has.
I know I can use "Switch" as well, but it doesn't change the situation.
Thanks in advance and a good day to everyone!!

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 landon912 · Sep 30, 2015 at 12:43 AM

You can modify some of the code to something like this:

 for(int i = 0; i < unitList.Count; i++)
 {
      unitList[i].GetComponent<Unit>.strength = anotherScript.unitStrengthValues[i];
 }

Here if you store the unit's strength values in an array, you can simply index them together. I also question why they're separate to begin with, but I digress. I hope this helps.

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 Caiovvs · Sep 30, 2015 at 01:17 AM 0
Share

It works!! Thank you very much!

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

28 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

Related Questions

how to controll a variable of a script from another script 2 Answers

Constructing the name of the variable being accessed 1 Answer

Interchangeable animations in a script? 0 Answers

Basic Enemy follow script for Unity five, desperately needed! 0 Answers

why does my enemy instant spawn bullets 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