Wrong result after calling an instance

Hello guys,

I’m having a problem on getting some resutls.I have 2 scenes.In the first scene i have my player with his stats and in the second scene i want my player to fight a pc bot player with some stats i have given to it.The problem is that my player is always winning even if the pc bot player is stronger.Any ideas?
Here is the code:

 public int level;
    public string name;
    public int health;
    public int attack;
    public int def;

    public int atc; 
    public int df;
    public int hp;

    public void OnMouseDown()
    {
        atc = statsTextsToDisplayAndChanges._INstance.attack;
        df = statsTextsToDisplayAndChanges._INstance.def;
        hp = statsTextsToDisplayAndChanges._INstance.health;


        if (attack+def+health <= atc+df+hp)
        {
            Debug.Log("YOU WON");
        }
        else
        {
            Debug.Log("YOU LOST");
        }
    }

What is in your statsTextsToDisplayAndChanges script?

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

public class statsTextsToDisplayAndChanges : MonoBehaviour
{
    public static statsTextsToDisplayAndChanges _INstance;
    buyWeapons bw;
    public Text m_LevelText;
    public Text m_AttackText;
    public Text m_DefendText;
    public Text m_MoneyText;
    public int level;
    public int money;
    public int health;
    public int attack;
    public int def;
    string answer;
    string url = "http://alex3685.dx.am/display.php";
    private GameObject _Manager; 
    public int sword1 = 200;
    public int sword2 = 300;
    public int sword3 = 400;
    public int sword4 = 600;
    public int sword5 = 750;
    public int sword6 = 900;
    public int shield1 = 200;
    public int shield2 = 200;
    public int shield3 = 200;
    public int shield4 = 200;
    public int shield5 = 200;
    public int shield6 = 200;

    public void Start()
    {
        if (_INstance == null)
        {

            _INstance = this;
            DontDestroyOnLoad(this.gameObject);
            _Manager = GameObject.Find("playerStats"); // gia na allazei tis metavlites otan agorazei apo to shop o xristis!
        }
        else if (_INstance != this)
        {
            Destroy(gameObject);
        }
        display();


    }
public void display()
    {


        m_LevelText.text = level.ToString();
        m_AttackText.text = attack.ToString();
        m_DefendText.text = def.ToString();
        m_MoneyText.text = money.ToString();
    }

The stats are given in the inspector thats why you just see public int attack; instead of public int attack=10;

Who is the player and who is the bot? Gotta be something amiss here, since the logic is correct (presuming the order is correct)… :slight_smile:

Hey methos you came to save the day again? :stuck_out_tongue: The player is in the statsTextsToDisplayAndChanges script

and the bot is here

lol well I just don’t see how that’s not working. If you print out the values in the function for MouseDown, what does it print out? This should be pretty simple :slight_smile:

it prints only “you won” no matter what :stuck_out_tongue:

hahaha. No, no man… What values for the 6 variables there does it print out? :stuck_out_tongue: haha

oh sorry :stuck_out_tongue:
hmm the bot stats are all zero

Sweet… so, there’s a problem :wink:

sweet that we have a cause, at least… now why are they zero is next :wink:

i added some values for attack def and health on my function OnMouseDown and it works now.Strange though that it didn’t worked when i added the values from the inspector.Thanks for the tip though it helped me solve it :smile:

 public void OnMouseDown()
    {
        atc = statsTextsToDisplayAndChanges._INstance.attack;
        df = statsTextsToDisplayAndChanges._INstance.def;
        hp = statsTextsToDisplayAndChanges._INstance.health;

        health = 1000;
        attack = 1000;
        def = 1000;

        if (attack+def+health <= atc+df+hp)
        {
            Debug.Log("YOU WON");
            Debug.Log("Players stats:" + atc + df + hp);
            Debug.Log("Bot stats:" + attack + def + health);

        }
        else
        {
            Debug.Log("YOU LOST");
            Debug.Log("Players stats:" + atc + df + hp);
            Debug.Log("Bot stats:" + attack + def + health);
        }
    }

Well, that’s only sort of good , because it should work the other way lol.
When you’re in that scene and you look in the inspector, are the values still correct while you’re playing?

I mean , if it’s working well enough you can forget about it… but if you want to try to solve it more, it’s up to you :slight_smile:

I will try to solve it yeah but it’s ok for now i want to finish it :smile: Once again you helped me thanks for that :slight_smile:

Okay, sure, no problem. Enjoy :slight_smile: