Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Boolean not able to be set

Discussion in 'Scripting' started by cheapcpps, Nov 13, 2021.

  1. cheapcpps

    cheapcpps

    Joined:
    Jul 15, 2020
    Posts:
    53
    I have a script that when you click a sprite, it sets a boolean in another class to give you an item.

    I also have a script in Update() that should will hide or show the Sprite Renderer under certain circumstances.
    However, in the script that gives you an item, I also want to set the boolean, (in the same class) show, to false. At first I thought there was an issue with the script in update, but the truth is that shows was never being changed at all. I have no clue why it isn't changing shows, I tried putting it after my switch case, before, and in it.

    Code (CSharp):
    1. public void OnMouseOver()
    2.     {
    3.         if (Input.GetMouseButtonDown(0))
    4.         {
    5.             switch (itemToGive)
    6.             {
    7.                 case 1:
    8.                     hb.item1 = true;
    9.                     shows = false;
    10.                     break;
    11.                 case 2:
    12.                     hb.item2 = true;
    13.                     shows = false;
    14.                     break;
    15.                 case 3:
    16.                     hb.item3 = true;
    17.                     shows = false;
    18.                     break;
    19.                 default:
    20.                     hb.item1 = true;
    21.                     shows = false;
    22.                     break;
    23.             }
    24.         }
    25.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Review the docs for OnMouseOver() to make sure you're meeting ALL the requirements to be called.

    Beyond that, you must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,527
    How exactly did you gather that "truth"? The easiest way to debug such issues is by temporarily replacing your boolean variable with a property. So assuming you currently have

    Code (CSharp):
    1. public bool show;
    in your class you could directly replace it with

    Code (CSharp):
    1.  
    2. private bool m_Show;
    3. public bool show
    4. {
    5.     get => m_Show;
    6.     set {
    7.         Debug.Log("show is set from " + m_Show + " to the new value of " + value + " on GO: " + gameObject.name, gameObject);
    8.         m_Show = value;
    9.     }
    10. }
    This will generate a log message every time show gets a value assigned. This will tell you:

    • when and from where the variable is changed.
    • what the old and new values are.
    • On which gameobject this script is attached to
    - Each log message has a complete stack trace so you can see exactly which method and which line will actually change the show property. In addition we pass the gameObject as second parameter to Log. This will make Unity highlight the exact object in the editor when you click on the log message in the console. This helps to identify issues where you may execute this code on the wrong object.
     
  4. cheapcpps

    cheapcpps

    Joined:
    Jul 15, 2020
    Posts:
    53
    You didn't have to respond to this at all because the issue was fixed and the project was abandonded, my reason for my script not working was so specific and unrelated to the title that it wouldn't help anyone, the issue was unrelated to my script, my script worked fine, it had something to do with an issue of a collider iirc.