Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MapuHoB · Oct 13, 2014 at 07:55 AM · variablestatic

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 ?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Oct 13, 2014 at 11:02 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
     }
 }



Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MapuHoB · Oct 13, 2014 at 09:28 AM 0
Share

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!

avatar image HarshadK · Oct 13, 2014 at 09:33 AM 0
Share

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.

avatar image MapuHoB · Oct 13, 2014 at 09:43 AM 0
Share

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

avatar image Bunny83 · Oct 13, 2014 at 11:34 AM 1
Share

@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.

avatar image HarshadK · Oct 13, 2014 at 11:38 AM 1
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

Setting static variables in Editor script 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges