Static variable not changing from outside class

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour
{
#region variables

public const int MaxBrainGamesPerTunnel = 3;

#region static
public static float GameSpeed;
public static int BrainGamesPlayed = 0;
public static bool GameOver, IsGamePaused;
#endregion

public GameObject blackScreen;
public GameObject GameWorld;
#endregion

void Awake()
{
    GameSpeed = 13 * PlayerPrefs.GetFloat("ScaleY"); //editor
    BlackScreenManager.IsTotallyBlack = false;

    //print(PlayerPrefs.GetInt("CameraWidth") + "			" + PlayerPrefs.GetInt("CameraHeight"));
}

void Update () {
    CheckForGameOver();
}

void CheckForGameOver()
{
    if (GameOver)
    {
        blackScreen.SetActive(true);
        GameSpeed *= 0.9f;

        if (BlackScreenManager.IsTotallyBlack)
        {
            Destroy(GameWorld);
            Application.LoadLevel("MainMenuScene");
        }
    }
}

void OnDestroy()
{
    GameOver = false;
}

public static void PauseGame()
{
    IsGamePaused = true;
}

public static void ResumeGame()
{
    IsGamePaused = false;
}

}

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

public class CheckIsCorrect : MonoBehaviour {

public static bool CanPlay = true;

private GameObject currentText;
private float timePassedAfterClick;

public GameObject correct;
public GameObject incorrect;
void Start () {
    this.currentText = GameObject.Find(transform.name + "Text");
    CanPlay = false;
    //print(GameManager.BrainGamesPlayed);
}

void Update () {
    Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

    this.timePassedAfterClick += (this.timePassedAfterClick > 0 ? Time.deltaTime : 0);

    if (this.timePassedAfterClick == 0)
    {
        if (collider2D.OverlapPoint(mousePos) && Input.GetMouseButtonDown(0))
        {
            this.timePassedAfterClick += Time.deltaTime;
            if (this.IsCorrect())
            {
                this.correct.SetActive(true);
            }
            else
            {
                this.incorrect.SetActive(true);
            }
        }
    }
    else if(this.timePassedAfterClick > 0.2f)
    {
        Destroy(transform.parent.gameObject);
    }
    
}

private bool IsCorrect()
{
    int currentNumber = int.Parse(currentText.GetComponent<Text>().text);

    if (currentNumber == EquationControler.Result)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void OnDestroy()
{        
    if (GameManager.BrainGamesPlayed == GameManager.MaxBrainGamesPerTunnel)
    {
        GameManager.ResumeGame();
    }
    else
    {
        CanPlay = true;
    }
}

}

Note: I make a print line to check if it enters OnCollisionStay and it does indeed but GamesPlayed static variable doesn’t seem to change at all. What can be the problem ?

@deadliness: Uhm where do you actually change "BrainGamesPlayed"?

2 Answers

2

for the way you are trying to access your class, your class also needs to be static.

Something like:

public static class GameManager : MonoBehaviour
{
    public static int GamesPlayed = 0;
}

Then you can access it like the way you are doing it:

public class CheckForGame : MonoBehaviour {
 
    void OnCollisionStay2D(Collision2D collision)
    {
        GameManager.GamesPlayed += 1;
    }
}

OR

an another method is using an instance of class that is static using which you can access your class variables. Something like:

public class GameManager : MonoBehaviour
{
    public static int GamesPlayed = 0;

    public static GameManager gameManager = null;

    void Awake()
    {
           if(gameManager == null)
           {
               gameManager = this;
               DontDestroyOnLoad(gameObject);
            } else
            {
               Destroy(gameObject);
            }

     }
}

Then you can access it like:

public class CheckForGame : MonoBehaviour {
 
    void OnCollisionStay2D(Collision2D collision)
    {
        // We use the gameManager instance of the class to access it's variable
        GameManager.gameManger.GamesPlayed += 1;
    }
}

I'm using some other variables which aren't static in the GameManager class and also Awake which doesn't allow me to make the class static. Anyway thanks for the immediate answer!

It will be helpful if you could provide the whole script so that we can provide some proper solution. The solution I provided was based on your script provided in question.

I corrected it now.I'm sorry I didn't previously but I think there are too many useless lines of code there and that's why I decided not to

@HarshadK: A class doesn't need to be static to have static members. The constraint is the otherway round: A static class can only have static members. A non static class can contain both, instance members and static members.

@Bunny83 You have a point but as stated in the comment above the method I posted in the answer was based on the partial code that he provided previously where he was actually accessing the class member by just using the class name directly like: GameManager.GamesPlayed = +1; Hence for this I provided that he needs to have a static class for accessing the class directly by name and not via an instance. Correct me if I'm wrong.

I’m not sure exactly how I managed to fix the bug, but I think it was accessed from more than 2 classes and in one of them I change it and then in the other I put it back to normal … hah sry for the inconvinient question