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

Question Double Coins with button

Discussion in 'Scripting' started by Knello, Oct 24, 2020.

  1. Knello

    Knello

    Joined:
    Jul 27, 2020
    Posts:
    50
    Hello, I need help with my code. I wanna make a button which doubles the coin which the player collected.
    So now my problem is if the player hast collected the coins and is at the finish screen there are the coins showed but if I klick the button the coins don't double and nothing happens.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7.  
    8. public class ScoreCounter : MonoBehaviour
    9. {
    10.     [SerializeField]
    11.     public TMP_Text doubleScoreText;
    12.  
    13.     public int scoreValue;
    14.    
    15.    
    16.    
    17.        
    18.     public void Update()
    19.     {
    20.        
    21.         doubleScoreText.text = "Rewards: " + scoreValue;
    22.     }
    23.  
    24.     private void OnCollisionEnter2D(Collision2D other)
    25.     {
    26.         string tag = other.collider.tag;
    27.  
    28.         if (tag.Equals("Coins"))
    29.         {
    30.             scoreValue += 25;
    31.         }
    32.            
    33.  
    34.     }
    35.    
    36.     public void DoubleCoins()
    37.     {
    38.        
    39.         GameDataManager.AddCoins(scoreValue);
    40.         GameSharedUI.Instance.UpdateCoinsUIText();
    41.     }
    42. }
    43.  
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    for that we would need to see whats inside your
    Code (CSharp):
    1. GameDataManager.AddCoins(scoreValue);
    function :)
     
  3. Knello

    Knello

    Joined:
    Jul 27, 2020
    Posts:
    50
    As you can set I got it from another script normaly between the brackets are numbers but I dont know how to do it in this case I wanna take the amount of coins which the player has collected and double this

    Code (CSharp):
    1. public static void AddCoins(int amount)
    2.     {
    3.         playerData.coins += amount;
    4.         SavePlayerData();
    5.  
    6.     }
     
    Last edited: Oct 24, 2020
  4. Knello

    Knello

    Joined:
    Jul 27, 2020
    Posts:
    50
    So can you help me with my issue?
     
  5. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    Is your function getting called?

    maybe let it log if it reaches that part of code to see if it gets called
     
  6. Knello

    Knello

    Joined:
    Jul 27, 2020
    Posts:
    50
    Yes, the function gets called everytime i click the button the console shows PlayerDataSaved
     
  7. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    hmm thats weird,
    what about scoreValue, is it bigger then 0 ?