- Home /
C# bug with a class boolean ?
I have a gamemanager class in C# :
public class GameManager : MonoBehaviour
{
....
....
bool perdu = false;
public void aPerdu()
{
perdu = true;
Debug.Log("aPerdu "+perdu);
}
void Update()
{
Debug.Log("Update:" + perdu);
)
I call the function aPerdu() to set the boolean to true. Nowhere else in the code the boolean is set to a value (true or false).
In my trace i have a "aPerdu true" and in Update it keep stating that perdu is false. i do not understantd how can my aPerdu change scope is limited to the function.
This is something i done thousands of times in Java, how can c#/unity act differently? What am i doing wrong?
PS : Sorry for the missing closing brackets but alt gr + the key next to backspace (fr keyboard) generate a enter code here
instead of closing bracket
i pasted that code and just added to call aPerdu() on mouse click and it is working fine.
are you Instantiating this Component or did you added it to gameObject in editor?
Ok i tried to set my boolean to static... and now it works. It works with this change but i'd really like to know why it does not work if it is not a static. $$anonymous$$y script is attached to a Gameobject and it is a singleton:
` using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Game$$anonymous$$anager : $$anonymous$$onoBehaviour
{
#region Fields
private int buttonWidth = 250;
private int buttonHeigtht = 25;
private List<I$$anonymous$$bant> Est$$anonymous$$obile = new List<I$$anonymous$$bant>();
private int angleactuel = 0;
private static volatile Game$$anonymous$$anager _instance;
private static object _lock = new object();
private static bool perdu = false;
#endregion
#region Properties
#endregion
#region Functions
static Game$$anonymous$$anager() {} //Stops the lock being created ahead of time if it's not necessary
public static Game$$anonymous$$anager Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null) _instance = new Game$$anonymous$$anager();
}
}
return _instance;
}
}
void Start()
{
foreach (GameObject gameobj in FindObjectsOfType(typeof(GameObject)) as GameObject[])
{
if (gameobj.GetComponent(gameobj.name) is I$$anonymous$$bant){
Est$$anonymous$$obile.Add((I$$anonymous$$bant)gameobj.GetComponent(gameobj.name));
}
}
_instance = this;
//perdu = false;
}
void Update()
{
Debug.Log("OOOOOOOOOOOOOOOOO :" + perdu);
}
void OnGUI()
{
if (perdu==true)
{
Debug.Log("HAAaaAazazazAAAAAAAAAAAAA :" + perdu);
if (GUI.Button(new Rect(50, 50, buttonWidth, buttonHeigtht), "Recommencer le niveau"))
{
Application.LoadLevel(Application.loadedLevel);
}
}
if (GUI.Button(new Rect(10 ,Screen.height -10- buttonHeigtht , buttonWidth, buttonHeigtht), "<-")){
rotationNiveau(-90);
}
if(GUI.Button(new Rect(Screen.width - 10 - buttonWidth, Screen.height - 10 - buttonHeigtht, buttonWidth, buttonHeigtht), "->")){
rotationNiveau(90);
}
}
private void rotationNiveau(int zangle){
angleactuel += zangle;
Physics.gravity =Quaternion.Euler(0, 0, zangle) * Physics.gravity;
foreach (I$$anonymous$$bant tombant in Est$$anonymous$$obile)
{
tombant.Tourne(zangle);
}
}
//met le booléen perdu a vrai
public void aPerdu()
{
perdu = true;
Debug.Log("Houps! "+perdu);
}
#endregion
}
Answer by Ryu_Fanel · Jun 27, 2011 at 08:48 AM
the answer to your problem is very simple. nearly all of your methods are inherited by MonoBehaviour. Start(), Update() a.s.o. will be called automatically in unity. your method aPerdu() doesn't exist in MonoBehaviour, so you have to call it in one of the other methods.
For Example:
void Update()
{
aPerdu();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612093459im_/https://answers.unity.com/themes/thub/images/avi.jpg)