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

A bit of help with statements and lists.

Discussion in 'Scripting' started by FritzPeace, Mar 8, 2015.

  1. FritzPeace

    FritzPeace

    Joined:
    Sep 15, 2014
    Posts:
    29
    Hello. I have a game where I want the background to change color with bigger scores. I have 5 colors, so want to have each 25 points the background changes.
    How could I do it a simplified way?
    My idea would be to have a list that gets all the numbers between 0 and 25, 100 and 125, 200 and 225 and like this it would go.
    Then another one from 26 to 50, 126 to 150 etc.
    I just do not know how I would do this and if this is the correct way.
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Storing all the numbers wouldn't be good. You should find a rule/function which applies to your needs and describes the color's index from 1 to n (0 to n-1, respectively).
    The only "list" (is this case an array is enough) that i'd use is the one which holds the different colors.
    Making it public will allow you to set the colors in the inspector as you may already know.

    Next, have a method (in C# you could also use a property) which adjusts the score. Within the method, first add or set the score. Secondly, in order to find the correct background color, you can do some basic math (assuming score and range are integers, if not, only some small adjustments have to be made):

    (score/range)%colorArray.Length;

    score is selfdescriptive, range would be 25 in your case and colorArray.Length is the number of colors.
    The result will be the color index.

    Some examples:
    (13/25)%5 = 0%5 = 0 // first color
    (29/25)%5 = 1%5 = 1 // second color
    (67/25)%5 = 2%5 = 2 // third color
    ...
    (124/25)%5 = 4%5 = 4
    (150/25)%5 = 6%5 = 1
    (193/25)%5 = 7%5 = 2

    as you can see it starts all over again, and it's an equally distributed pattern.

    There are tons of other ways, of course, but i personally like this one.
     
    FritzPeace likes this.
  3. FritzPeace

    FritzPeace

    Joined:
    Sep 15, 2014
    Posts:
    29
    I already have a score and I am using custom made colors. I have 5 of them. How would be the code? I want that
    00-20 is color 1;
    21-40 is color 2;
    41-60 is color 3;
    61-80 is color 4;
    81-100 is color 5;
    And how does an Array work?
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I know you already have a score, as i don't know your implementation i've added one really basic one by myself so you can test it and get an idea of how it works.
    In this sample, Awake, Update, OnGUI are just for testing purposes, you'll have your own systems to increment the score etc.


    - score: the currentScore will be stored here
    - scoreToAdd: just for the test case, change it in the inspector to increase the score by greater values instead of only 1
    - backgroundColors: you can choose your own background colors in the inspector
    - range: the actual range per color (0 to range-1; range to (2*range-1) etc) E.g 20: 0 - 19, 20 - 39, 40 - 59

    Awake(): just add zero once to make sure the background color will be set before you score the first time
    Update(): checks for spacebar and adds 'scoreToAdd' to the current score
    OnGUI(): just displays your current score for testing purposes

    AddScoreAndChangeBGColor(int amount): adds amount to the current score and calculates the color index + sets the background color of the main camera (using Unity 5 you may need to click on the camera and choose 'solid color' in the 'Clear Flags' drop down menu to see an effect.

    Code (CSharp):
    1. public class Test: MonoBehaviour
    2. {
    3.     private int score;
    4.     public int scoreToAdd = 1;
    5.  
    6.     public Color[] backgroundColors;
    7.     private int range = 20;
    8.  
    9.     public void AddScoreAndChangeBGColor(int amount)
    10.     {
    11.         score += amount;
    12.         int colorIndex = (score / range) % backgroundColors.Length;
    13.         Camera.main.backgroundColor = backgroundColors[colorIndex];
    14.     }
    15.  
    16.     void Awake() { AddScore(0); }
    17.     void Update()
    18.     {
    19.         if (Input.GetKeyDown(KeyCode.Space))
    20.         {
    21.             AddScore(scoreToAdd);
    22.         }
    23.     }
    24.  
    25.     void OnGUI()
    26.     {
    27.         GUILayout.Label("Score: " + score.ToString());
    28.     }
    29. }
    Please don't double/triple post. :p
    *edit I renamed the class, sorry for that. I simply used one that i didn't need anymore and the name didn't fit at all. haha ^^
     
    FritzPeace likes this.
  5. FritzPeace

    FritzPeace

    Joined:
    Sep 15, 2014
    Posts:
    29
    But how do I change the backgroundColors.Lenght;?
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You do not change it directly, or let's say, that's the number of elements in your array so you specify it when creating a new array.

    Usually, an array can be created with:
    Code (CSharp):
    1. Color[] myArray = new Color[10];
    Calling myArray.Length will return 10, so the number of 'slots' in this array.

    As we have a public Color array in this script, you won't neccessarily need that line because Unity allows you to specify the number of elements in the inspector. You can change the 'size' there and it will update in the inspector, showing you the amount of slots you've entered.

    *Edit, this is how it looks like
     
    FritzPeace likes this.