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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Scripting - need help to make my script shorter

Discussion in 'Scripting' started by NextVertex, Sep 26, 2016.

  1. NextVertex

    NextVertex

    Joined:
    Sep 26, 2016
    Posts:
    28
    Hi,
    I'm not very good in scripting, i've never made c# before using Unity.
    So i try to do my best but sometimes, it's not very compact and light.

    So i'm here to ask your help about this code, that i would like to make shorter / more compact.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Coins : MonoBehaviour
    5. {
    6.       public float cash;
    7.  
    8. voidUpdate(){
    9.       if(TileManager.score==1)
    10.       {
    11.             if(TileManager.score==3)
    12.             {
    13.                   if(TileManager.score==7)
    14.                   {
    15.                         if(TileManager.score==10)
    16.                         {
    17.                               something like cash = cash + 100
    18.                         }
    19.  
    20. ETC ... ... ... ...
    21.  
    22.                         else
    23.                         {
    24.                               something like cash = cash + 150
    25.                         }
    26.                   }
    27.                   else
    28.                   {
    29.                        something like cash = cash + 50
    30.                   }
    31.             }
    32.             else
    33.             {
    34.                  something like cash = cash + 30
    35.              }
    36.       }
    37.       else
    38.       {
    39.             something like cash = cash + 10
    40.       }
    41.  
    Maybe my code is wrong, i have not test it yet because i don't want to have this huge code if another way, more compact, to make it exist.

    Thank you in advance for your help, have a nice day :)
     
    Last edited: Sep 26, 2016
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Please use code tags http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    While you could use switch statements instead of if/else, the real question is what are you trying to do. Your code is very hard to read right now without the indentions also.

    If all you want to do is add cash based on score, if it's a simple formula
    Code (CSharp):
    1. cash += 10 * score;
    Should do the trick also. Assuming that formula always works out. Otherwise, if you have various amounts, you may need to change the reward amount

    Code (CSharp):
    1.  
    2. if(score < 10)
    3.    reward = 10;
    4. else
    5.    reward = 20;
    6. cash += reward * score;
     
    NextVertex likes this.
  3. NextVertex

    NextVertex

    Joined:
    Sep 26, 2016
    Posts:
    28
    Thank you for your answer !
    Yes that's approximatly what i want to do.
    But the problem is that i'm not very good in Unity, and i don't realy know how to execute this script only at the end of the game. The code i wrote before was executed during the game to add continuesly cash during the game and not at the end (because i don't know how to do it).
    But thank you a lot for your answer, i will try to do like this.

    If somebody have another answer, i will be happy too :)
     
  4. Pharaoh_

    Pharaoh_

    Joined:
    Feb 13, 2014
    Posts:
    36
    If you have values that do not follow some logic (e.g. a formula of the type (TileManager.score * 5) + 10)), I suggest you create a dictionary.

    Code (csharp):
    1. using System.Collections.Generic;
    2.  
    3.  
    4.     public Dictionary<int, int> scoreCashManager = new Dictionary<int, int>() {
    5.         {1, 10}, {3, 30}, {7, 50}, {10, 150}
    6.     };
    7.  
    8.     public int _Cash = 0;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         int reward = 0;
    13.         if (scoreCashManager.TryGetValue (TileManager.score, out reward)) {
    14.             _Cash += reward;
    15.         }
    16.         Debug.Log (_Cash);
    17.     }
    18.  
    Bear in mind that you cannot reuse the same key value in a KeyValue pair, which is how Dictionary operates (basically, you cannot find the same dictionary entry twice (e.g. in a real dictionary, the word "father" would not appear twice)). However, for your purpose and provided that you do not aim for a formula, this will be a good solution.

    Basically, you store pairs of integers in a dictionary. Thus, the code first checks if the score from the TileManager exists in the dictionary as an entry and, if it does, it will add its corresponding cash value to your "_Cash" variable.
     
    Last edited: Sep 26, 2016
    phocker and NextVertex like this.
  5. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    I'd use a for loop here... something like:

    Code (csharp):
    1.  
    2. for(int i = 0; i < maxValue; i++)
    3. {
    4.     if(TileManager.score == i)
    5.     {
    6.         cash = i * someValue;
    7.     }
    8. }
    9.  
     
    NextVertex likes this.
  6. NextVertex

    NextVertex

    Joined:
    Sep 26, 2016
    Posts:
    28
    Thank you for your answer ! I'm not very skilled in the developpement, so i think i will choose a different way to make what i want. But i realy want to say Thank you for you complete and fast answer. I will try to learn more about your method !
     
  7. NextVertex

    NextVertex

    Joined:
    Sep 26, 2016
    Posts:
    28
    Thank you ! I will try your method too, it's realy more compact than what i've done before lol.
    Thank you again ! :)
     
    MD_Reptile likes this.
  8. phocker

    phocker

    Joined:
    Sep 12, 2010
    Posts:
    57
    This is by far the best scenario.

    Another alternative would be to create a ScriptableObject with the same dictionary so that you can update it without having to recompile your code.
     
    NextVertex likes this.
  9. NextVertex

    NextVertex

    Joined:
    Sep 26, 2016
    Posts:
    28
    Thank you guys for your answers ! I will test them soon, but i'm pretty sure that it will solve my problem. Thank you again for your help ! ;) Have a nice day.
     
    Pharaoh_ likes this.