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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Using GetComponent doesn't give updated bool

Discussion in 'Scripting' started by Severin_Suveren, Apr 18, 2020.

  1. Severin_Suveren

    Severin_Suveren

    Joined:
    Apr 16, 2020
    Posts:
    2
    I have 2 scripts attached to the same game object and I'm trying to access a bool in one from the other.
    I'm able to check the bool in script1 from script2 through getcomponent, but it doesn't update the value when it changes. I suspect it's only checking the value in the inspector and not the script itself, and so it's always the same.

    Is anyone able to help with this? I've spent countless hours searching the web and looking at videos trying to solve this. I've seen some guides setting the bool to private, but then I get errors that script2 is unable to access it.

    What I'm trying to do is play animations in script1 on keyinputs, then change the behaviour of the player in script2 (ie movementspeed), and for that to work I need to know if the conditions are met (true/false) in script1.

    Here are the scripts: (btw script1 = BasicAnimations and script2 = PlayerMovement). Line 64 in the 2nd script is what I can't get it to do.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BasicAnimations : MonoBehaviour
    6. {
    7.  
    8.     static Animator anim;
    9.    
    10.     public bool isRunning;
    11.    
    12.    
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         anim = GetComponent<Animator>();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.        
    24.         if (Input.GetButtonDown("Jump"))
    25.         {
    26.             anim.SetTrigger("isJumping");
    27.         }
    28.        
    29.         if (Input.GetButton("Vertical"))
    30.         {  
    31.             anim.SetBool("isWalking", true);
    32.                 //Debug.Log("isWalking True");
    33.             anim.SetBool("isIdle", false);
    34.                
    35.         }
    36.         else
    37.         {
    38.             anim.SetBool("isWalking", false);
    39.             anim.SetBool("isRunning", false);
    40.             anim.SetBool("isIdle", true);
    41.                 //Debug.Log("isIdle True");
    42.                 //Debug.Log("BasicAnimations isRunning false");
    43.         }
    44.        
    45.  
    46.         if (Input.GetButton("Run") && (Input.GetButton("Vertical")))      
    47.         {
    48.             anim.SetBool("isRunning", true);
    49.             anim.SetBool("isWalking", false);
    50.                 //Debug.Log("BasicAnimations isRunning true");
    51.         }
    52.        
    53.          
    54.     }
    55. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6.  
    7. {
    8.  
    9.  
    10.     public CharacterController controller;
    11.     public BasicAnimations boolValue;
    12.  
    13.     public float walkspeed = 2f;
    14.     public float runspeed = 4f;
    15.     public float gravity = -9.81f;
    16.     public float jumpHeight = 1f;
    17.  
    18.     public Transform groundCheck;
    19.     public float groundDistance = 0.4f;
    20.     public LayerMask groundMask;
    21.  
    22.  
    23.  
    24.     Vector3 velocity;
    25.     bool isGrounded;
    26.  
    27.  
    28.  
    29.     // Start is called before the first frame update
    30.     void Start()
    31.     {
    32.      
    33.         boolValue = GetComponent<BasicAnimations>();
    34.            
    35.     }
    36.  
    37.    
    38.     // Update is called once per frame
    39.     void Update()
    40.     {
    41.  
    42.        
    43.      
    44.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    45.  
    46.         if (isGrounded && velocity.y < 0)
    47.             {
    48.                 velocity.y = -2f;
    49.             }
    50.        
    51.        
    52.        
    53.  
    54.         float x = Input.GetAxis("Horizontal");
    55.         float z = Input.GetAxis("Vertical");
    56.  
    57.         Vector3 move = transform.right * x + transform.forward * z;
    58.  
    59.         controller.Move(move * walkspeed * Time.deltaTime);
    60.  
    61.        
    62.      
    63.        
    64.          if (boolValue.isRunning == true)  
    65.         {
    66.            Debug.Log("PlayerMovement isRunning true");
    67.            //controller.Move(move * runspeed * Time.deltaTime);
    68.         }
    69.        
    70.        
    71.         if (boolValue.isRunning == false)
    72.         {
    73.             Debug.Log("PlayerMovement isRunning false");
    74.         }
    75.  
    76.        
    77.  
    78.         if (Input.GetButtonDown("Jump") && isGrounded)
    79.         {
    80.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    81.         }
    82.  
    83.         velocity.y += gravity * Time.deltaTime;
    84.  
    85.         controller.Move(velocity *Time.deltaTime);
    86.     }
    87. }
    88.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You are never changing the value of "isRunning" in your BasicAnimations script. Calling "anim.SetBool()" will set a value on your animation component, but it won't change the value of the isRunning field in your script.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    If you want to access isRunning from your animation try this. Replace
    Code (CSharp):
    1. public bool isRunning;
    with
    Code (CSharp):
    1. public bool isRunning => anim.GetBool("isRunning");
     
  4. Severin_Suveren

    Severin_Suveren

    Joined:
    Apr 16, 2020
    Posts:
    2
    Solved all my problems, thank you!