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 djgaven588 · Mar 01, 2017 at 04:25 AM · shaderprefabintconvertoptimize

Turning a int into a possibility of 19 different outcomes

As the question says. I have a script currently working as such. If said number is equal to another. Do something. Code: void ConvertToPrefab(int[] convert) { toSpawn = new GameObject[convert.Length]; for (int i = 0; i < convert.Length; i++) { if (convert[i] == 0) { toSpawn[i] = prefabs.cubes[0]; } else if (convert[i] == 1) { toSpawn[i] = prefabs.cubes[1]; } } and so on. How might I go about just having a few lines instead of somewhere of about 80? This would really help since this code is used often and can be ran with about 500 different integers at once. I feel like this is causing a little hiccup and would like to find a better solution.

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
0

Answer by Commoble · Mar 01, 2017 at 07:48 PM

Firstly, you can make your code samples easier to read here by using the Code Sample button just above the text entry box, which preserves tabs and line breaks:

 void ConvertToPrefab(int[] convert)
 {
     toSpawn = new GameObject[convert.Length];
     for (int i = 0; i < convert.Length; i++)
     {
         if (convert[i] == 0)
         {
             toSpawn[i] = prefabs.cubes[0];
         }
         else if (convert[i] == 1)
         {
             toSpawn[i] = prefabs.cubes[1];
         }
     }
 }


Secondly, I'm a little confused as to what you're trying to accomplish. The loop you have above can be simplified like this:

 void ConvertToPrefab(int[] convert)
 {
     toSpawn = new GameObject[convert.Length];
     for (int i = 0; i < convert.Length; i++)
     {
         toSpawn[i] = prefabs.cubes[convert[i]];
     }
 }

This is assuming your int[] convert array is a jumble of random integers in no particular pattern, all of which are valid indices for your prefab array. If this isn't what you're looking for, we'll need more details about what you have and what you're trying to do.

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 djgaven588 · Mar 01, 2017 at 11:11 PM 0
Share

That worked but with slight modification, convert[i] needs to be ins$$anonymous$$d (convert[i]) unity thinks you are telling that array to do something other than tell information.

 void ConvertToPrefab(int[] convert)
     {
         toSpawn = new GameObject[convert.Length];
         for (int i = 0; i < convert.Length; i++)
         {
             toSpawn[i] = prefabs.cubes[(convert[i])];
         }
     }

Also, I have another void that is turning object names into integers. Is this possible without the same huge amount of code?

void ConvertToInt(GameObject[] convert) { for (int i = 0; i < convert.Length; i++) { if (convert[i].name == ("RedCube(Clone)")) { //Red Cube cubeId[i] = 0; }

     {

{

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

132 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

Related Questions

Having trouble converting this int to string, can someone help? 0 Answers

Convert "Double" to "Float" or "Int"? 1 Answer

cannot convert type HealthandInventory to int 0 Answers

Standard Asset prefabs only showing in game mode 0 Answers

Convert Hex (int) to Color/Color32? 5 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