Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Combining Variables?

Discussion in 'Scripting' started by LightWell, Oct 6, 2014.

  1. LightWell

    LightWell

    Joined:
    Sep 6, 2014
    Posts:
    43
    So, I'm not sure quite how to phrase this, but I was just trying to make a calculator, a good little exercise, and I found that I couldn't do it because of such a small issue, how would I combine variables, what I mean by this is I don't want to add or do maths or anything, I want to put two variables, and make them into one, kinda like this:
    var one = 12
    var two = 34
    var three = one+two (I want this to be 1234, not 46).
    Yes, I know I didn't add semi-colons or anything, but that's not the point, if you want more detail just ask, I really don't have a good way of explaining this.
     
  2. fbork

    fbork

    Joined:
    Oct 6, 2014
    Posts:
    5
    you probably would need to convert the numbers to strings, combine then and then create a number again
     
  3. LightWell

    LightWell

    Joined:
    Sep 6, 2014
    Posts:
    43
    How would I got about that, I understand how to change a variable to a string, but how would I change a string back into a variable?

    EDIT: Not tested yet, but would it just be as easy as:
    Code (JavaScript):
    1. var Number1 : int = 3;
    2. var Number2 : int = 6;
    3. var Num1String = Number1.ToString;
    4. var Num2String = Number2.ToString;
    5. var Combo = "Num1String"+"Num2String";
    6. var total = Combo;
     
    Last edited: Oct 6, 2014
  4. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    First, your line 5 is wrong. When combining strings variables, do not use quotation marks. Otherwise your result will literally be "Num1StringNum2String". If you want the string "36" just concatenate the variables, no quotes.

    You need an explicit conversion from string back to int. I THINK
    Code (csharp):
    1. myInt =int.Parse(myString);
    should do the trick, but I have not tested it myself.

    Also, ToString is a method and needs () to follow it.
     
  5. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    If you want Combo to just be an int that combines the other two ints instead than adding them:
    Code (csharp):
    1. var Number1:int = 3;
    2. var Number2:int = 6;
    3. var Combo:int = Convert.ToInt32(Number1.ToString() + Number2.ToString()); // 36
     
  6. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I think I know what you are after:
    Code (csharp):
    1. var three = one * 100 + two;
    If you were just adding a digit one at a time with button presses then you would always be multiplying by 10.

    If you want to concatenate two arbitrary numbers then you could use a string to perform the concatenation and then convert back again. Or you could calculate the number of digits in the first number.