Why it's working only the first time?

Hey people so i have the following code:

if (Result1.r1 < Result2.r2)
        {

            Application.LoadLevel("GamePlay");
        }

        else if (Result1.r1 > Result2.r2)
        {

            Application.LoadLevel("GameOver");

        }
        else if (Result1.r1 == Result2.r2)
        {

            Application.LoadLevel("GameOver");

        }
    }

It’s supposed to load the gameplay scene again if Result1<Result2, but it’s working only the first time after this it keeps loading the gameplay scene ,but it doesn’t check about the condition just keeps loading the scene does anyone have idea whats going on. Thank you.

I just realized whats going on it’s supposed to generate different numbers everytime,but after the first start it keeps generating the same numbers here is the whole script.

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

public class GameManager : MonoBehaviour {


    public GameObject youranimatableObject;
    private Animator yourAnimator;
    public Text Math1;
    public Text Math2;


    public class Xclass
    {
        public static int X = Random.Range(1, 21);
    }
    public class Yclass
    {
        public static int Y = Random.Range(1, 21);
    }
    public class Zclass
    {
        public static int Z = Random.Range(1, 21);
    }
    public class Wclass
    {
        public static int W = Random.Range(1, 21);
    }
    public class Result1
    {
        public static int r1 = Xclass.X + Yclass.Y;
    }
     public class Result2
    {
        public static int r2 = Zclass.Z + Wclass.W;
    }
   
    void Start () {




   
   

    Math1.text = Xclass.X + "+"+ Yclass.Y;
    Math2.text = Zclass .Z+ "+" + Wclass.W;
      

       

    }
  
   public void CheckLesser()
       
    {
        if (Result1.r1 < Result2.r2)
        {

            Application.LoadLevel("GamePlay");
        }

        else if (Result1.r1 > Result2.r2)
        {

            Application.LoadLevel("GameOver");

        }
        else if (Result1.r1 == Result2.r2)
        {

            Application.LoadLevel("GameOver");

        }
    }

Well, if this code doesn’t run at all, is the level being loaded somewhere else?
Or is this code being run but the variables are not what you think they are?

1 Like

That makes more sense… I guess you’re never making new random numbers? :slight_smile:

1 Like

Why do you have so all those classes just for some ints? Also, are you using an old version of Unity as Application.LoadLevel has been replaced with the scene manager.

You could do away with the classes;

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

public class GameManager : MonoBehaviour
{
    public GameObject youranimatableObject;
    private Animator yourAnimator;
    public Text Math1;
    public Text Math2;

    public static int X, Y, Z, W, r1, r2;

    private void GenerateRandomNumbers()
    {
        X = Random.Range(1, 21);
        Y = Random.Range(1, 21);
        Z = Random.Range(1, 21);
        W = Random.Range(1, 21);

        r1 = X + Y;
        r2 = Z + W;
    }

    void Start()
    {
        GenerateRandomNumbers();
        Math1.text = X + "+" + Y;
        Math2.text = Z + "+" + Y;
    }

    public void CheckLesser()

    {
        if (r1 < r2)
        {
            Application.LoadLevel("GamePlay");
        }

        else if (r1 > r2 || r1 == r2)
        {
            Application.LoadLevel("GameOver");
        }
    }
}
1 Like

Thank you for the quick answer guys.
WarmedxMints That helped thank you :slight_smile:

But for some reason out of random when i press the correct button it loads the gameover scene. Any help why this could be happening.

No idea, there isn’t anything to do with buttons in there so I don’t know what they are doing in your game.

I’m sorry i haven’t posted the whole script:

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

public class GameManager : MonoBehaviour
{
    public GameObject youranimatableObject;
    private Animator yourAnimator;
    public Text Math1;
    public Text Math2;

    public static int X, Y, Z, W, r1, r2;

    private void GenerateRandomNumbers()
    {
        X = Random.Range(1, 21);
        Y = Random.Range(1, 21);
        Z = Random.Range(1, 21);
        W = Random.Range(1, 21);

        r1 = X + Y;
        r2 = Z + W;
    }

    void Start()
    {
        GenerateRandomNumbers();
        Math1.text = X + "+" + Y;
        Math2.text = Z + "+" + Y;
    }

    public void CheckLesser()

    {
        if (r1 < r2)
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        }

        else if (r1 > r2 || r1 == r2)
        {
            Application.LoadLevel("GameOver");
        }
    }
    public void CheckGreater()

    {
        if (r1 > r2)
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        }

        else if (r1 < r2 || r1 == r2)
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        }
    }
    public void CheckEqual()

    {
        if (r1 == r2)
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        }

        else if (r1 > r2 || r1 < r2)
        {
            Application.LoadLevel("GameOver");
        }
    }
}

CheckLesser,CheckGreater,CheckEqual are buttons. Thank you.

Your functions don’t make much sense tbh.

From what I can see, you are trying to display to equations on the screen and then the player has to decide if the sum of the first is either greater than, less than or equal to the sum of second?

If they are correctly, they get another question and if they are wrong it is game over?

That’s exactly what i’m trying to do. For some reason it’s working properly few times then it just mistakes and loads the game over scene.

Math2.text = Z + “+” + Y;

Should be Z + “+” + W; typo ?

1 Like

Oh my gosh… once again thank you so much!

:slight_smile: np.

You can shorten your script a bit. Just point your buttons to CheckAnswer and pass an int of 1, 2 or 3. Look at the enum to see which button should send which value.

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

public class GameManager : MonoBehaviour
{
    public Text Math1;
    public Text Math2;
    public static int X, Y, Z, W, r1, r2;

    private enum Answer
    {
        LessThan = 1,
        Equal = 2,
        GreaterThan = 3
    };

    private Answer answer;

    private void GenerateRandomNumbersAndAnswer()
    {
        X = Random.Range(1, 21);
        Y = Random.Range(1, 21);
        Z = Random.Range(1, 21);
        W = Random.Range(1, 21);

        r1 = X + Y;
        r2 = Z + W;

        if (r1 < r2)
        {
            answer = Answer.LessThan;
        }
        else if (r1 == r2)
        {
            answer = Answer.Equal;
        }
        else if (r1 > r2)
        {
            answer = Answer.GreaterThan;
        }

        Math1.text = X + "+" + Y;
        Math2.text = Z + "+" + W;
    }

    void Start()
    {
        GenerateRandomNumbersAndAnswer();

    }

    public void CheckAnswer(int _answer)
    {
        if ((Answer)_answer == answer)
        {
            GenerateRandomNumbersAndAnswer();
        }
        else
        {
            //Load Game Over Scene
            Math1.text = "Game";
            Math2.text = "Over";       
        }
    }
}
1 Like

Thank you! :slight_smile: