- Home /
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") + "\t\t\t" + 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"?
Answer by MapuHoB · Dec 12, 2014 at 02:54 AM
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
Answer by HarshadK · Oct 13, 2014 at 08:03 AM
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 Game$$anonymous$$anager 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
@Harshad$$anonymous$$: And that's what i said: The class don't need to be static. This is perfecly fine:
public class SomeClass : $$anonymous$$onoBehaviour
{
public string someInstanceField; // instance member
public static string aStaticField; // static member
void Update()
{
// ...
}
}
And somewhere else:
void DoSomething()
{
SomeClass.aStaticField = "Hello World!";
}
Have you actually looked at your own singleton example? It's a non static class with a static member field which you access like:
Game$$anonymous$$anager.game$$anonymous$$anager
I just said that the class doesn't need to be a static class if you want to use static members. Static members work exactly the same, no matter if it's a static class or not. Again, a static class just enforces that all members are static. You can't create an instance of a static class. So a static class that inherits from $$anonymous$$onoBehaviour doesn't make any sense and i actually doubt that it would work since $$anonymous$$onoBehaviour isn't a static class.
Oooops! Didn't think it that way. Thank's for clarifying it. >.< I was actually under impression that class needs to be static in order to be accessed directly. Need to read the static class manual for C# carefully now.
Your answer
Follow this Question
Related Questions
Increasing a score value? 2 Answers
Static Variable Problem 1 Answer
Public and Static Variables 2 Answers
Is it possible to show Static Variables in the Inspector? 10 Answers