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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Percentatge Source Code

Discussion in 'Scripting' started by dadansumardani, Mar 7, 2020.

  1. dadansumardani

    dadansumardani

    Joined:
    Nov 26, 2018
    Posts:
    4
    Dear Developer

    I need your help How do I add numbers to "totalSuccess" to be multiplied by some of number? I want to multiply this "TotalSuccess" by 1000.

    My source code

    void OnGUI()
    {
    http://GUI.Box (new Rect(0, 0, 0, 0), totalSuccess.ToString ());
    }

    Similar with this source code!
    ScoreText.text = Score.ToString();

    I want to add the multiplied number to Score by 1000. Can you help me?

    Thanks You
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    I dont think i understood you correctly, but if you want score to be totalSuccess multiplied by 1000, then it's simply
    Code (CSharp):
    1. int score = totalSuccess * 1000;
    You can also just write (totalSuccess*1000).ToString() into your current GUI code, but i'd rather keep it modular.
    If this is not what you were asking for you may need to restructure the question.
     
    Last edited: Mar 7, 2020
  3. DoggyBoggy

    DoggyBoggy

    Joined:
    Jan 19, 2018
    Posts:
    20
    I'm not quite sure what you're asking, and correct me if this inst what you wanted. If you want to add only a single number, the is just a simple equation, and if i didn't misunderstand the problem, its a very beginner problem, not that that's bad, we all have to start somewhere. So ill try my best to document my code for you =). Here is the add one number variant of the code:

    totalSuccess = (NUM_YOU_WANT_TO_ADD + totalSuccess) * NUM_YOU_WANT_TO_MULTIPLY_BY;


    but if you want to add multiple numbers, you could do something like this:

    Code (CSharp):
    1. //The start to the function you had above
    2. void OnGUI()
    3. {
    4.     //This is an integer array, basically a list of a bunch of whole numbers you want to do something with, in this case, you want to add all the numbers inside to the 'totalSuccess' variable
    5.     int[,] NUMS_TO_ADD = new int[,] {ADD_NUMBERS_HERE_SEPARATED_BY_COMMAS};
    6.     //We are setting the totalSuccess variable to zero because its value will get changed when we add the numbers from the list to it, unless you want to add these numbers to another number, then change this from zero.
    7.     int totalSuccess = 0;
    8.  
    9.     //The loop in this circumstance goes through each number in the array of numbers you just made
    10.     foreach(int number in NUMS_TO_ADD)
    11.     {
    12.         //When its going through each number in the array, it saves it to a variable which updates each loop to represent the number its on in the list, and we add each number in the list o the 'totalSuccess' variable
    13.         totalSuccess += number;
    14.     }
    15.     //Finally, we repeat the code that you had above, except we multiply the 'totalSuccess' variable by the number you want to multiply by before turning it into a string
    16.     GUI.Box(new Rect(0, 0, 0, 0), (totalSuccess*NUM_TO_MULTIPLY_BY).ToString ());
    17. }
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4.   GUI.Box(new Rect(0, 0, 0, 0), (totalSuccess * 1000).ToString());
    5. }
    6.  
     
  5. dadansumardani

    dadansumardani

    Joined:
    Nov 26, 2018
    Posts:
    4
    Alright, thanks you, it work
     
  6. dadansumardani

    dadansumardani

    Joined:
    Nov 26, 2018
    Posts:
    4
    It work using (totalSuccess*100).ToString, Thanks you
     
  7. dadansumardani

    dadansumardani

    Joined:
    Nov 26, 2018
    Posts:
    4
    Actually, I use "total granular" and divide it with the number of area in unity, so it work when I devided by area . Thanks you