Search Unity

Bug Shrodingers variables.

Discussion in 'Editor & General Support' started by penguinslapp, Aug 3, 2022.

  1. penguinslapp

    penguinslapp

    Joined:
    Mar 21, 2022
    Posts:
    6
    I try to change a variable whenever I click a button, and the button thinks it's changed the variable, but anything that isn't the function that activates when pressed by the button doesn't detect a change. The variable can be 2 values at the same time and I have no idea how to solve this. I'm still pretty new to unity.
    Here's an example of the button function:
    Code (CSharp):
    1. public int i = 1;
    2. public void onButtonPush()
    3. {
    4.         i++;
    5.         Debug.Log(i); //returns the amount of times you've pushed it
    6. }
    and here's one of what's trying to access it:
    Code (CSharp):
    1. void Update()
    2. {
    3.         Debug.Log(FindObjectOfType<otherscript>().GetComponent<otherscript>().i); // returns 1
    4.  
    5. }
     
  2. penguinslapp

    penguinslapp

    Joined:
    Mar 21, 2022
    Posts:
    6
    I should note the variable i also retains its value if I start and stop the game.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Maybe you have more than one copy of the script in scene!

    Don't write code like the above.

    Be like a real Unity dev and make a
    public otherscript
    field and drag the intended instance in there.

    Way simpler, far fewer moving parts to reason about.

    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
    - you're getting an error or warning and you haven't noticed it in the console window

    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 supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    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, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    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
     
  4. penguinslapp

    penguinslapp

    Joined:
    Mar 21, 2022
    Posts:
    6
    Thanks for the great answer, I'll try throwing in some debug.logs and see if I can pinpoint the problem!