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 Hjalte · Sep 12, 2014 at 08:06 PM · c#arrayintget

I want some ints stored in a new int right after eachother

I have 9 ints that i want added together but instead of 1+2 = 3. I want them to stand next to eachother like this: 1, 2 = 12

Heres my code.

 public int item1;
     public int item2;
     public int item3;
     public int item4;
     public int item5;
     public int item6;
     public int item7;
     public int item8;
     public int item9;
 
     public GameObject[] slots;
     private GetItemNumber getNum;
 
     void OnMouseDown(){
         print ("ss");
 
         foreach (GameObject slot in slots) {
             getNum = slot.GetComponent<GetItemNumber>();
             getNum.SendNumbers();
         }
 
         Debug.Log (item1+""+item2+""+item3+""+item4+""+item5+""+item6+""+item7+""+item8+""+item9);
 
     }
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 rutter · Sep 12, 2014 at 08:15 PM

There's an easy but risky way to do this: string operations.

 int Concat(int x, int y) {
     string s = x.ToString() + y.ToString();
     return int.Parse(s);
 }

That gets the job done in a hurry.

If you want more control or efficiency, it's worth considering how our number system works.

What is 12? It's 10 plus 2:

 10 + 2
 (1)(10^1) + (2)(10^0)

What is 654? It's 600 + 50 + 4:

 600 + 50 + 4
 (6)(10^2) + (5)(10^1) + (4)(10^0)

Each digit is worth ten times the digit after it.

If you have a series of single-digit numbers, you can count them up. Add each digit, multiplying by ten as you go:

 int Concat(int[] digits) {
     int total = 0;
     for (int i=0; i<digits.length; i++) {
         if (i > 0) total *= 10;
         total += digits[i];
     }    
 }


That assumes you have single-digit numbers. Though, you could use a similar process in reverse to break any multi-digit number into single-digit numbers.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Converting a string to an int 2 Answers

Best Way to Load 10^8 int Array in Unity/C#? 1 Answer

Monodevelop is expecting int from my float array 3 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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