• 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 Sylario · Jun 01, 2011 at 02:54 PM · variableclass

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

Comment
Add comment · Show 3
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 iggy · Jun 01, 2011 at 03:05 PM 0
Share

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?

avatar image Sylario · Jun 01, 2011 at 05:18 PM 0
Share

he is attached to a gameObject

avatar image Sylario · Jun 01, 2011 at 09:18 PM 0
Share

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

}

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

C#: Declaring a script as a variable 1 Answer

AmmoPickup.js Help: Collision and edit Var from another class 1 Answer

How Reset Variable After Loop 1 Answer

Scriptable object gets deleted with a class that holds it? 1 Answer

Modify a variable inside a game object from other object. 1 Answer


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