Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I change public variables depending on which level? 1 Answer

How do you use a variable in a line of code? 1 Answer

to point a selfwritten class in the inspector (drag n drop) 0 Answers

How to store class type in variable 2 Answers

Access Variable in a class from another script 4 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