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

Help! Create multiplier PowerUp C#

Discussion in 'Scripting' started by Manfrix94, May 27, 2015.

  1. Manfrix94

    Manfrix94

    Joined:
    Jan 25, 2015
    Posts:
    3
    Hi everyone!

    I have a problem, I have to create a multiplier power up that, when I take an object in the scene, . the power up is enabled and I got double points. My problem is that this does not happen and I do not understand why. Currently I have a Score Manager that manages the steps of the various objects in the scene:
    Code (CSharp):
    1. public class ScoreManager : MonoBehaviour
    2. {
    3.  
    4.     void Start ()
    5.     {
    6.         PlayerPrefs.SetInt ("CurrentScore", 0);
    7.     }
    8.  
    9.     public void AddScoreBronze ()
    10.     {
    11.         int tempScore = PlayerPrefs.GetInt ("CurrentScore");
    12.         tempScore += 100;
    13.         PlayerPrefs.SetInt ("CurrentScore", tempScore);
    14.         gameObject.SetActive(false);
    15.  
    16.     }
    17.  
    18.     public void AddScoreSilver ()
    19.     {
    20.         int tempScore = PlayerPrefs.GetInt ("CurrentScore");
    21.         tempScore += 500;
    22.         PlayerPrefs.SetInt ("CurrentScore", tempScore);
    23.         gameObject.SetActive(false);
    24.  
    25.     }
    26.  
    27.     public void AddScoreGold ()
    28.     {
    29.         int tempScore = PlayerPrefs.GetInt ("CurrentScore");
    30.         tempScore += 1000;
    31.         PlayerPrefs.SetInt ("CurrentScore", tempScore);
    32.         gameObject.SetActive(false);
    33.  
    34.     }
    35. }
    The script that is attached to the object "Power Up":

    Code (CSharp):
    1. public class PowerUpX2 : MonoBehaviour
    2. {
    3.     //the Boolean is activated only when it collides with the player
    4.     bool onOff = false;
    5.  
    6.     GameObject[] powerX2Bronze;
    7.     GameObject[] powerX2Silver;
    8.     GameObject[] powerX2Gold;
    9.  
    10.     public float multiplierLength = 5f;
    11.  
    12.     void Start ()
    13.     {
    14.         if (powerX2Bronze == null && powerX2Silver == null && powerX2Gold == null)
    15.         {
    16.             //search all gameobject with that tag
    17.             powerX2Bronze = GameObject.FindGameObjectsWithTag ("scoreBronze");
    18.             powerX2Silver = GameObject.FindGameObjectsWithTag ("scoreSilver");
    19.             powerX2Gold = GameObject.FindGameObjectsWithTag ("scoreGold");
    20.         }
    21.     }
    22.    
    23.     void Update ()
    24.     {
    25.  
    26.         //when the Boolean is false, the script "Moltiplicatore" (script attached to all objects in the scene) is disabled
    27.         if (onOff == false)
    28.         {
    29.             for (int i = 0; i < powerX2Bronze.Length; i++)
    30.             {
    31.                 powerX2Bronze[i].GetComponent<Moltiplicatore>().enabled = false;
    32.             }
    33.  
    34.             for (int i = 0; i < powerX2Silver.Length; i++)
    35.             {
    36.                 powerX2Silver[i].GetComponent<Moltiplicatore>().enabled = false;
    37.             }
    38.  
    39.             for (int i = 0; i < powerX2Gold.Length; i++)
    40.             {
    41.                 powerX2Gold[i].GetComponent<Moltiplicatore>().enabled = false;
    42.             }
    43.         }
    44.  
    45.         //But when the Boolean is true, the script "Moltiplicatore" is enabled ...
    46.         else if (onOff == true)
    47.         {
    48.             for (int i = 0; i < powerX2Bronze.Length; i++)
    49.             {
    50.                 powerX2Bronze[i].GetComponent<Moltiplicatore>().enabled = true;
    51.             }
    52.  
    53.             for (int i = 0; i < powerX2Silver.Length; i++)
    54.             {
    55.                 powerX2Silver[i].GetComponent<Moltiplicatore>().enabled = true;
    56.             }
    57.  
    58.             for (int i = 0; i < powerX2Gold.Length; i++)
    59.             {
    60.                 powerX2Gold[i].GetComponent<Moltiplicatore>().enabled = true;
    61.             }
    62.  
    63.             //... and begins coroutine
    64.             StartCoroutine (TimerX2 ());
    65.             this.gameObject.SetActive(false);
    66.         }
    67.     }
    68.  
    69.     public IEnumerator TimerX2()
    70.     {
    71.         yield return new WaitForSeconds (multiplierLength);
    72.         onOff = false;
    73.     }
    74.  
    75.     //condition to become true Boolean
    76.     void OnCollisionEnter (Collision col)
    77.     {
    78.         if (col.gameObject.tag == "Player")
    79.         {
    80.             onOff = true;
    81.         }
    82.     }
    83. }
    And the script referenced "PowerUpX2":

    Code (CSharp):
    1. public class Moltiplicatore : MonoBehaviour
    2. {
    3.     int tempScorex2;
    4.  
    5.     bool multiply = false;
    6.  
    7.     void Update ()
    8.     {
    9.         if (multiply == true)
    10.         {
    11.             tempScorex2 = PlayerPrefs.GetInt("CurrentScore");
    12.             tempScorex2 *= 2;
    13.             Debug.Log(tempScorex2);
    14.             PlayerPrefs.SetInt ("CurrentScore", tempScorex2);
    15.         }
    16.     }
    17.     void OnCollisionEnter (Collision collid)
    18.     {
    19.         if (collid.gameObject.tag == "Player")
    20.         {
    21.             multiply = true;
    22.         }
    23.     }
    24. }
    25.  
    Thank you and sorry for my English! Ciao!
     
  2. Deleted User

    Deleted User

    Guest

    That's a lot of code... One thing that could help you debug it is making less redundant code like this:

    Code (CSharp):
    1. public class ScoreManager : MonoBehaviour
    2. {
    3.     void Start ()
    4.     {
    5.         PlayerPrefs.SetInt ("CurrentScore", 0);
    6.     }
    7.     public void AddScore (int amount)
    8.     {
    9.         int tempScore = PlayerPrefs.GetInt ("CurrentScore");
    10.         tempScore += amount;
    11.         PlayerPrefs.SetInt ("CurrentScore", tempScore);
    12.         gameObject.SetActive(false);
    13.     }
    14. }
     
    Manfrix94 and Fajlworks like this.
  3. legendz25

    legendz25

    Joined:
    May 30, 2014
    Posts:
    17
    Screenshot of the inspector for the gameobject that contains all of these scripts would be nice and may help in seeing the issue.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Static methods would help you out here, I bet. if you make it "public static void AddScore", then you wouldn't need a reference to the ScoreManager obejct - you could, from anywhere in your code, call ScoreManager.AddScore(100). Since you are just using PlayerPrefs and not any local (e.g. on the gameobject/component) storage of data, you don't even have to change any of your function.

    And taking this a step further, a multiplier is as easy as:
    Code (csharp):
    1.  
    2. public static float multiplier = 1f;
    3. public static void AddScore(int amount) {
    4. int tempScore = PlayerPrefs.GetInt ("CurrentScore");
    5.         tempScore += amount * multiplier;
    6.         PlayerPrefs.SetInt ("CurrentScore", tempScore);
    7.         gameObject.SetActive(false);
    8.     }
    9.  
    10. // in some other script
    11. ScoreManager.multiplier = 2f;
    12. ScoreManager.AddScore(100); //actually adds 200
    13.  
     
    Manfrix94 likes this.
  5. Manfrix94

    Manfrix94

    Joined:
    Jan 25, 2015
    Posts:
    3
    First thanks a lot for the fastest replies. I've tried the things you said and the static method works fine and simplify the code.

    But the problem is always there. I don't understand why only declaring ScoreManager.multiplier and the ScoreManager.AddScore() method he would add 2. I've tried to put it in my code but he do nothing at all. Only the normal function to add scores.
     
  6. Manfrix94

    Manfrix94

    Joined:
    Jan 25, 2015
    Posts:
    3
    Thanks for the reply, i followed your and StarManta advice, that helped to simplify the code!