user5491906
user5491906

Reputation:

C# if statement not being excuted unity

Im working on a shop where user can increase the damage of his weapon when he clicks on a button the current damage is displayed along with his money which is stored as "score" in playerprefs in the editor i ave set my money or "score" to be 80 i want it to cost 45 money everytime user wants to increase damage by 5 this is my script but for some reason its not working

using UnityEngine;
using System.Collections;

public class IncreaseDamage : MonoBehaviour 
{
    private int damage;
    private int math;
    private float money;
    private float math2;
    
    void Update()
    {
        money = PlayerPrefs.GetFloat("score");
    }
    
    public void increaseDamage()
    {
        if(money >= 45)
        {
            damage = PlayerPrefs.GetInt("damage");
            math = damage + 5;
            PlayerPrefs.SetInt("damage",math);
            math2 = money - 45;
            PlayerPrefs.SetFloat("score",math2);
        }
    }
}

EDIT: i have made scripts which test the button and it works fine it stopped working once i added the if statement

other important scripts

This gets the players current damage it is attached to a UIText

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GetPlayerDamage : MonoBehaviour {
    Text text;
    private int damage;
    
    void Awake(){
        text = GetComponent<Text>();
    }
    
    void Update(){
        damage = PlayerPrefs.GetInt("damage");
        text.text = "Damage: " + damage;
    }
}

this manages the score or money(same thing)

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ScoreManager : MonoBehaviour
{
    public static int score;
    Text text;

    void Awake()
    {
        text = GetComponent<Text>();
        score = PlayerPrefs.GetInt("score");
    }

    void Update ()
    {
        text.text = "Score: " + score;
        PlayerPrefs.SetInt("score", score);
    }
}

this displays the money in a text to the user

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MoneyManager : MonoBehaviour {
    public static int score;
    Text text;

    void Awake()
    {
        text = GetComponent<Text>();
    }

    void Update ()
    {
        text.text = "Money: " + PlayerPrefs.GetInt("score");
    }
}

Upvotes: 0

Views: 115

Answers (2)

user5491906
user5491906

Reputation:

I realized the issue i was getting score as a float when i should have been getting it as an int

heres the updated(and correct) code

using UnityEngine;
using System.Collections;

public class IncreaseDamage : MonoBehaviour {
    private int damage;
    private int math;
    private int money;
    private int math2;

    void Update(){
        money = PlayerPrefs.GetInt("score");
    }

    public void increaseDamage(){
        if(money >= 45){
            damage = PlayerPrefs.GetInt("damage");
            math = damage + 5;
            PlayerPrefs.SetInt("damage",math);
            math2 = money - 45;
            PlayerPrefs.SetInt("score",math2);
        }
    }
}

Upvotes: 0

Puddler
Puddler

Reputation: 2939

You haven't initialized money.

Although there is not enough information given in the question I'm going to assume you need to pull the score before you calculate if(money >= 45) with something like money = PlayerPrefs.GetFloat("score");

using UnityEngine;
using System.Collections;

public class IncreaseDamage : MonoBehaviour {
     private int damage;
     private int math;
     private float money;
     private float math2;

     void Update(){
         money = PlayerPrefs.GetFloat("score");
     }

     public void increaseDamage(){
         money = PlayerPrefs.GetFloat("score");
         if(money >= 45){
             damage = PlayerPrefs.GetInt("damage");
             math = damage + 5;
             PlayerPrefs.SetInt("damage",math);
             math2 = money - 45;
             PlayerPrefs.SetFloat("score",math2);
         }
     }
 }

EDIT: Or you could call Update() before calling increaseDamage() in your button

Upvotes: 1

Related Questions