Search Unity

Variable value changing between methods

Discussion in 'Scripting' started by oliver_unity892, Jan 4, 2020.

  1. oliver_unity892

    oliver_unity892

    Joined:
    Oct 28, 2019
    Posts:
    91
    Hi all

    I have an odd issue where a variable is changing from False to True between methods without being changed anywhere in the script. The variable is set to True at the start, and remains True unless changed by a public method.

    The variable is being changed to FALSE correctly by the public method, but when it gets back to the Update() method the value has changed back to TRUE.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float moveSpeed = 5f;
    8.     public Rigidbody2D rb;
    9.  
    10.     public Animator animator;
    11.     internal bool AllowMove;
    12.     Vector2 movement;
    13.  
    14.     private void Start()
    15.     {
    16.         AllowMove = true;
    17.     }
    18.  
    19.  
    20.     public void StopMoving()
    21.     {
    22.         AllowMove = false;
    23.  
    24.         Debug.LogError("STOP MOVING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    25.         Debug.LogError(AllowMove);
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         if (AllowMove==true)
    31.         {
    32.             movement.x = Input.GetAxisRaw("Horizontal");
    33.             movement.y = Input.GetAxisRaw("Vertical");
    34.  
    35.             animator.SetFloat("Horizontal", movement.x);
    36.             animator.SetFloat("Vertical", movement.y);
    37.             animator.SetFloat("Speed", movement.sqrMagnitude);
    38.             rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    39.         }
    40.     }
    41. }
    42.  
    The code allows the player object to move while AllowMove is True. When the StopMoving method is called (by an external script) the player is meant to stop. The variable is changing to False correctly, but when execution gets to the Update method the value has gone back to True and the player can still move.

    It's exactly the same as this post, but detaching the script hasn't worked here.
    https://forum.unity.com/threads/pas...om-public-function-to-update-function.510611/


    Any ideas?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Remove the
    internal
    keyword and make the
    AllowMove
    variable a getter/setter, then do a
    Debug.Log()
    in the setter and you can see in the Unity console where it was called from.

    What I mean goes something like this:

    Code (csharp):
    1. bool _AllowMovement; // nobody touch this!!! this will "backing store" our variable
    2. bool AllowMovement
    3. {
    4.   get
    5.   {
    6.     return _AllowMovement;
    7.   }
    8.   set
    9.   {
    10.     Debug.Log( "Set to " + value);
    11.     _AllowMovement = value;
    12.   }
    13. }
    Run it, study your console log, it should reveal what is going on, as well as give you a time log of that variable's value.
     
  3. oliver_unity892

    oliver_unity892

    Joined:
    Oct 28, 2019
    Posts:
    91
    Thanks for that. It doesn't really reveal anything we didn't know unfortunately.

    The console shows my public method being used, and the SET from your code setting _allowmovement to FALSE.

    However the IF(ALLOWMOVEMENT) {} still returns true and the ALLOWMOVEMENT bool doesn't have it's SET triggered fired again at all. So it's Set to False, and Update immediately returns True without any other Set calls.

    I've gone through the other scripts (it's not a large project at the moment) and that public method isn't called anywhere else, other than the object I know is meant to call it, and it's only being called once.

    It kinda feels like a Unity bug so I might upgrade my version. Currently at 2019.2.13f1

    Olly
     
  4. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    Code (CSharp):
    1. private void Start()
    2. {
    3.     Debug.Log("Starting PlayerController");
    4. ...
    How many times does the log show up?
     
  5. oliver_unity892

    oliver_unity892

    Joined:
    Oct 28, 2019
    Posts:
    91
    Right. The Start method is only called once. I have that line in there, along with a variable that increments each time it's called and the variable stays at 1.

    What I see now is this:

    Console2.png

    The StopMoving method is called, setting the AllowMove variable to False.
    The SET method of AllowMove is called, confirming it's going to False.

    However the Update function still shows that AllowMovement==True.

    A breakpoint on AllowMovement immediately after the Set shows that it's True, despite Set previously (the frame before) reporting it's False.

    Set isn't called in between times.

    Odd?
     
  6. eliyah

    eliyah

    Joined:
    May 4, 2019
    Posts:
    6
    I Don't know if this is gonna do difference but test replace
    Code (CSharp):
    1. if (AllowMove==true)
    with
    Code (CSharp):
    1. Debug.Log(AllowMove);
    2. if (AllowMove)
     
  7. oliver_unity892

    oliver_unity892

    Joined:
    Oct 28, 2019
    Posts:
    91
    Ah already tried that. Didn't work.

    Also tried AllowMove!=FALSE too and that didn't work. I've also tried changing the logic so that AllowMove becomes DontAllowMove, but it doesn;t work.

    I don't feel that I'm doing anything wrong here. It just feels like something is broken.
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Change your update debug message to
    Debug.Log("This only shows when AllowMovement==True", gameObject);

    Then, when the messages print out in the console, click on them and see if it highlights the gameobject you expect it to.

    You can also search for your script and make sure you don't have duplicates. Pretty sure @Hikiko66 may have been trying to hint at this.

    I honestly don't believe this is a bug or more would be experiencing it.
     
  9. oliver_unity892

    oliver_unity892

    Joined:
    Oct 28, 2019
    Posts:
    91
    Thanks.

    I've added that. When I click on the error output it highlights the Player object, which is the object which has the PlayerController script assigned to it.

    I just don't get how it's even possible that a variable can change value without the SET function being called.
     
    Last edited: Jan 6, 2020