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 /
  • Help Room /
avatar image
0
Question by Subject-Gabz · Dec 06, 2016 at 11:36 AM · c#performanceif-statementsefficiency

Less code and avoiding unecessary If-statements

Hello everyone.

For example, let's say I've this code:

 public void SetSpotsSprites () {
     for(int i = 0; i< AvailableVehicles.Count; i++){
         if(activeVehicle.name == AvailableVehicles[i].vehicleName){

             if(AvailableVehicles[i].spot01Weapon != null)
                 spotsSprites[0].GetComponent<Image>().sprite = Resources.Load <Sprite> ("StoreSprites/Weapons/"+AvailableVehicles[i].spot01Weapon);
             

             if(AvailableVehicles[i].spot02Weapon != null)
                 spotsSprites[1].GetComponent<Image>().sprite = Resources.Load <Sprite> ("StoreSprites/Weapons/"+AvailableVehicles[i].spot02Weapon);
             
                     
             if(AvailableVehicles[i].spot03Weapon != null)
                 spotsSprites[2].GetComponent<Image>().sprite = Resources.Load <Sprite> ("StoreSprites/Weapons/"+AvailableVehicles[i].spot03Weapon);
     }
 }

Is there a way to make that code more compact? Something like this:

 public void SetSpotsSprites () {
     for(int i = 0; i< AvailableVehicles.Count; i++){
         if(activeVehicle.name == AvailableVehicles[i].vehicleName){

             if(AvailableVehicles[i].spot0[i]Weapon != null)
                 spotsSprites[0].GetComponent<Image>().sprite = Resources.Load <Sprite> ("StoreSprites/Weapons/"+AvailableVehicles[i].spot0[i]Weapon);
         }
 }

Thanks!

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 doublemax · Dec 07, 2016 at 08:46 AM 1
Share

Ins$$anonymous$$d of having individual methods for each weapon, add a method that returns the weapon.

 Vehicles.spot01Weapon -> Vehicles.GetWeapon(0)
 Vehicles.spot02Weapon -> Vehicles.GetWeapon(1)
 Vehicles.spot03Weapon -> Vehicles.GetWeapon(2)

avatar image Subject-Gabz doublemax · Dec 09, 2016 at 12:37 PM 0
Share

That actually may come in handy in a very near future. Thanks!

2 Replies

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

Answer by AlwaysSunny · Dec 06, 2016 at 11:56 AM

Always a good idea to be thinking about ways to reduce code. There's never a one-size-fits-all answer to questions like this, though. It's about developing good practices for structuring your data. For instance, as you hinted at, you could have an array of whatever these spot01Weapon objects are. This is an extremely common and clever way to handle logical groups of objects, and having logically grouped objects in collections is almost always a smart thing to do. Then you'd be addressing them by index instead of by their variable name. Steps like this are a good habit, but you can also overdo it. Have a look at C# Generic Lists too. You'll get the hang of it!

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 Subject-Gabz · Dec 06, 2016 at 02:26 PM 0
Share

Yes I'm already familiarized with Generic Lists. I was just trying to know if when sometimes, by mistake, we come to the situation i pointed above and if there was a simple solution. But apparently no. So I guess I'll have to change a couple of things in my game.

Thanks anyway!

avatar image Subject-Gabz · Dec 06, 2016 at 02:32 PM 0
Share

I'll accept your comment if you put it as a reply.

avatar image
1

Answer by UnityCoach · Dec 06, 2016 at 12:18 PM

You would have to put your spotXXWeapon in a List, then you could do another loop through it.

 public void SetSpotsSprites ()
 {
      for(int i = 0; i< AvailableVehicles.Count; i++)
      {
          if(activeVehicle.name == AvailableVehicles[i].vehicleName)
          {
              for (int w = 0 ; w<AvailableVehicles[i].spotWeapons.Count ; w++)
              {
                   // TODO : add your stuff here
              {
          }
      }
  }
Comment
Add comment · Show 3 · 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 Subject-Gabz · Dec 06, 2016 at 02:29 PM 0
Share

Not quite what i had in $$anonymous$$d. $$anonymous$$ore loops isn't a valid solution at this moment.

But thanks anyway.

avatar image Owen-Reynolds · Dec 09, 2016 at 07:09 PM 1
Share

The idea here is the standard "use an array ins$$anonymous$$d" one. You can turn spot01, spot02 and spot03 into an array. Inside the vehicle struct, replace Weapon spot01, spot02, spot03; with Weapon[] Spot;.

That gives all the usual benefits: can easily add more weapons, a loop with one body is less error-prone than multiple identical bodies, can treat it as a single object for functions.

Nested loops are good here because they show what you really have: many vehicles, many weapons for each vehicle. But don't push yourself to learn something new if you're on a deadline, or just want to get something finished. $$anonymous$$nowing this stuff will be worth it, but plan for several days at least, and a "try new ideas, that won't work at first" project. I've even got a chapter on array in struct in array ... in taxesforcatses.

avatar image Subject-Gabz Owen-Reynolds · Dec 12, 2016 at 04:05 PM 0
Share

That's actually what I end up doing. I put all the weaponsSlot in a Weapon[] Slot, and iterate trough them. But I thought the answer from AlwaysSunny was more $$anonymous$$d opening, and for future wanderers they may also find it quite useful. As well as the example given by you and jikkou.

Thanks!

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

283 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Low framerate on Android devices 0 Answers

Horrible Lag spike when Looking at an object that's showing GUI 1 Answer

AudioSource in Update 1 Answer

Project Spark to C# - Started To, While, and No Longer 1 Answer

can't check if my NPC has a specific script attached 0 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