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 okan-sen · Feb 27, 2020 at 10:18 AM · updatebooleannot workingreferencing

Boolean From Another Class Not Being Updated

Hello, I'm trying to implement a rather simple different modes system into my game. The game starts in "Build Mode" where players can move around using camera in top down view. When the player clicks a button they get into the "Character Mode" where they can move their character in thrid person.

So all I need is a simple boolean to keep track of the modes. I have declared the "inCharacterMode" variable in one class. Where we change it as we click on the button. The change works (tested).

The problem is; when I start the scene, the initiated boolean is called from the other class correctly, but somehow I cannot update it at all. The boolean stays the same in the calling class. But it changes in the declared class.

    public bool inCharacterMode = false;
        
     
     
         public void ChangeViewsButton()
         {
             if (inCharacterMode)
             {
                 Debug.Log("Character Mode: " + inCharacterMode);
                 inCharacterMode = !inCharacterMode;
             }
             else
             {
                 Debug.Log("Character Mode: " + inCharacterMode);
                 inCharacterMode = !inCharacterMode;
             }
                 
                 
         }
 
 


The calling/referencing class:

 void MoveCamera()
             {
                 if (GetComponent<UINewGame>().inCharacterMode == false)
                 {
                       .
                       .
                       .
                       .
                  }
             }


inCharacterMode boolean is not being passed on to the second class somehow. I would appreciate any help. Thanks in advance.

Comment
Add comment · Show 9
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 andrew-lukasik · Feb 27, 2020 at 10:10 AM 0
Share

GetComponent<UINewGame>().ChangeViewsButton();? Just so you know - you code, as it is now, is not passing anything.

avatar image okan-sen · Feb 27, 2020 at 03:14 PM 0
Share

Thanks for your reply. Do you mean that I should call the method not the variable?

avatar image andrew-lukasik okan-sen · Feb 27, 2020 at 05:06 PM 0
Share

Yes. Also - i find your code confusing. Your english says one thing but your c# says something different. == operator checks for boolean equality between LHS and RHS values, and definitely it's not changing anything.

avatar image andrew-lukasik okan-sen · Feb 27, 2020 at 05:09 PM 0
Share

if (GetComponent<UINewGame>().inCharacter$$anonymous$$ode == false) translated into plain english means: If field "inCharacter$$anonymous$$ode" in my component of type UINewGame has value "false" then ...

avatar image okan-sen · Feb 27, 2020 at 05:30 PM 0
Share

Yeah, sorry my messed up explanation. So, there's that button method which changes the boolean from true to false or false to true (It is in UINewGame class). And there's another class, where the $$anonymous$$oveCamera method exists. $$anonymous$$oveCamera should only read "inCharacter$$anonymous$$ode", not change it at all. According to the boolean being false, it should enter that if statement. If it is false, then it should not use the moveCamera method. And btw, since the changeViewsButton method is void I cannot use it for bool comparison. And I do not need it to be boolean type since it should not return anything.

avatar image andrew-lukasik okan-sen · Feb 27, 2020 at 09:46 PM 0
Share

Try something this: https://pastebin.com/ChFfCywh . Putting gameobjects in second argument of Debug.Logs will make that gameobject selected when you click on that log message in console - giving you the ability to figure who is calling who exactly. It's possible there is more than one instance of your class out there.

avatar image okan-sen andrew-lukasik · Feb 28, 2020 at 08:45 AM 0
Share

That was what I thought of too. $$anonymous$$y code might be creating multiple instances. I'll try what you've suggested when i get back home.

Another issue is, the button method only works twice, then it stops outputting debugs. Im getting confused about the user friendliness of Unity. Ive written codes in many languages but still I cannot comprehend the simplest thing in here.

Show more comments

3 Replies

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

Answer by ATLGAN · Feb 28, 2020 at 02:03 PM

You can make static of 'inCharacterMode' variable. If you do this you can call whenever you want easly:

 void MoveCamera()
                      {
                          if (UINewGame.inCharacterMode == false)
                          {
                                .
                                .
                                .
                                .
                           }
                      }



Comment
Add comment · Show 1 · 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 okan-sen · Feb 29, 2020 at 10:43 AM 0
Share

Thanks for your answer! This was the solution. I changed the variable that I wanted to be read to

public static bool

Then created a private static bool in the other class that needed to read the value.

Now the system works, thanks for everyone's time.

avatar image
0

Answer by tormentoarmagedoom · Feb 27, 2020 at 03:26 PM

Hello.

Why this "if" and "else" do exactly the same?

          if (inCharacterMode)
          {
              Debug.Log("Character Mode: " + inCharacterMode);
              inCharacterMode = !inCharacterMode;
          }
          else
          {
              Debug.Log("Character Mode: " + inCharacterMode);
              inCharacterMode = !inCharacterMode;
          }

This will do exactly the same:

public void ChangeViewsButton() { Debug.Log("Character Mode was: " + inCharacterMode); inCharacterMode = !inCharacterMode; }

And then, about your problem. you say "The boolean stays the same in the calling class. But it changes in the declared class." I dont get what you mean... The inCharacterMode "lives" in the first script. The second script just read the value of that variable, but is never "inside it", just read it from the 1st script...

Do this to see what's the value of the bool when trying to read it from the camera script:

 void MoveCamera()
              {
                  Debug.Log ("From Camera is: " + GetComponent<UINewGame>().inCharacterMode);
                  if (GetComponent<UINewGame>().inCharacterMode == false)
                  {

If is not what is suposed to bem is because there is anothor script changing its value.

Comment
Add comment · Show 1 · 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 okan-sen · Feb 27, 2020 at 05:05 PM 0
Share

Thanks for your reply. I added that Debug Log line inside the method before posting the question here and what it showed was, always the same state of the boolean. If started as False then it was always False, and vice versa.

What I need is, the "inCharacter$$anonymous$$ode" variable will change very often during gameplay, and when it is True, I want that particular code segment to not work. I'll add a new camera method for when it is True.

Problem is, I can't read the boolean from the other class whenever I want.

avatar image
0

Answer by Zentiu · Feb 28, 2020 at 05:31 PM

If a button you click gets you into 'Character mode' then why not make a method for that and let the button call for it and visa versa let anothet button call for the oposite? The method itself wll handle all the logic that you want to make the transition happen.

Comment
Add comment · Show 1 · 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 okan-sen · Feb 29, 2020 at 11:06 AM 0
Share

The camera method is called every frame in another class, I want the buttons to be in a separate class for a more organized setting.

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

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

the type or namespace name 'FirstPersonController' could not be found. 0 Answers

Changing a boolean value on button press not acting as expected? 1 Answer

Referencing a boolean from another script 1 Answer

Why is InvokeRepeating not working in boolean method? 0 Answers

Update collisions 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