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 crowman1337 · May 06, 2014 at 01:58 AM · c#booleanboolchanging

C# Boolean Constantly Changing

I am trying to alter a script that is not mine, but my boolean variable dead is not working correctly. The function CameraDeath() is called from a different script and correctly runs a single time, and the Debug.Log in that function prints only once proving that. However, after that function is called, my boolean constantly switches between true and false. The Debug.Log constantly running in the Update function goes back and forth, printing true and false for the value of dead. I have absolutely no idea why. I hope you can help.

 public class MouseLook : MonoBehaviour {
 
     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
     public RotationAxes axes = RotationAxes.MouseXAndY;
     public float sensitivityX = 15F;
     public float sensitivityY = 15F;
 
     public float minimumX = -360F;
     public float maximumX = 360F;
 
     public float minimumY = -60F;
     public float maximumY = 60F;
 
     public bool dead = false;
     public bool deadFunctionRan = false;
 
     float rotationY = 0F;
 
     void Update (){
 
         Debug.Log(dead);
 
         if (axes == RotationAxes.MouseXAndY && !dead) {
                 float rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX;
                 rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
                 transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
         } else if (axes == RotationAxes.MouseX && !dead) {
                 transform.Rotate (0, Input.GetAxis ("Mouse X") * sensitivityX, 0);
         } else if(!dead) {
                 rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
                 transform.localEulerAngles = new Vector3 (-rotationY, transform.localEulerAngles.y, 0);
         }
     }
 
     void CameraDeath(){
         dead = true;
         Debug.Log("function ran");
     }
 
     void Start (){
         // Make the rigid body not change rotation
         //if (rigidbody)
         //    rigidbody.freezeRotation = true;
     }
 }
Comment
Add comment · Show 4
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 mattyman174 · May 06, 2014 at 02:02 AM 0
Share

Is the "dead" variable accessed anywhere outside of the class?

If so, please post the class that is accessing it.

avatar image Jeff-Kesselman · May 06, 2014 at 02:03 AM 0
Share

Yes, you could start by making dead private.

The compiler will then give you an error everywhere else that is accessing it.

avatar image crowman1337 · May 06, 2014 at 02:04 AM 0
Share

No, dead is only being accessed inside the script posted and privatizing the variable gives no errors at compilation or at runtime.

avatar image crowman1337 · May 06, 2014 at 03:16 AM 0
Share

$$anonymous$$attyman's comment is correct. The script was attached to two objects. Unfortunately, I can't change this, so a different solution to my problem is going to have to be devised. Thank you.

1 Reply

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

Answer by mattyman174 · May 06, 2014 at 02:25 AM

Do you possibly have 2 MouseLook scripts attached to a GameObject or on any other GameObject? This could cause that type of output where one has called the CameraDeath() Function and its instance of the dead variable is set to true whilst the other still remains false.

It may look as though your bool is switching, but its just outputting the different values to the console.

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 mattyman174 · May 06, 2014 at 03:17 AM 0
Share

Can you accept this as correct to close this Question. Thanks.

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

22 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

Related Questions

GetComponent, set boolean to true but it willn't revert 3 Answers

Need help accessing booleans from other script. 2 Answers

c# Ignoring conditional statement? 1 Answer

What does this mean? 1 Answer

How can i reverse all the booleans in a method? 2 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