Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Boolean in Another Script always returns false

Discussion in 'Scripting' started by Silasbla, Apr 1, 2023.

  1. Silasbla

    Silasbla

    Joined:
    Oct 7, 2020
    Posts:
    6
    Hey guys,

    I'm messing around with my two scripts and an boolean variable.
    Script A is a player script which wants to check if the variable "spearInAir" in script B (Spear Prefab) is true/false. But the Calling method "isSpearInAir" always returns false, althought the inspector and the debug line of script B says the bool is true. Here the code:

    Script A (Player)
    Code (CSharp):
    1. public Spear spearScript;
    2.  
    3. private void Update
    4. {
    5.     if (Input.GetMouseButtonDown(1))
    6.     {
    7.          if (!spearInHand && !IsApearInAir())
    8.          {
    9.                 CreateNewSpear();
    10.                 spearInHand = true;
    11.  
    12.           }
    13.      }
    14. }
    15.    
    16. private bool IsApearInAir()
    17.     {
    18.         return spearScript.spearInAir;
    19.     }
    Script B (Spear)
    Code (CSharp):
    1. public bool spearInAir;
    2.     void Update()
    3.     {
    4.         Debug.Log("Spear says: " + spearInAir);
    5.  
    6.          if (Input.GetMouseButtonUp(0))
    7.         {
    8.             Throw();
    9.         }
    10.     }
    11.     public void Throw()
    12.     {
    13.         spearInAir = true;
    14.         rbSpear.bodyType = RigidbodyType2D.Dynamic;
    15.         rbSpear.gravityScale = 1;
    16.         rbSpear.AddForce(direction * launchForce, ForceMode2D.Impulse);
    17.     }
    18.  



    Like I said, the Update Method of the spear is always returns me the correct status (false per default and true after "Throw" is called. But the Player Script (A) always returns false.

    What am I missing? The connection in the inspector is set.

    Thanks for help!
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,656
    I'm guessing the answer is somewhere in the methods you didn't show us. What is
    CreateNewSpear()
    ? Where do you actually update the reference in
    spearScript
    ? It would be to your advantage to show the whole code.
     
  3. Silasbla

    Silasbla

    Joined:
    Oct 7, 2020
    Posts:
    6
    Hey @RadRedPanda thanks for your reply. Youre right, sorry. Guess is pretty hard to help if you can't see everything.
    Here are the full Scripts. I quite new so I guess I missing a complete essential point I cant find.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TestPlayerControl : MonoBehaviour
    6. {
    7.     [Header("Moving Variables")]
    8.     public bool isFacingRight = true;
    9.     public float movementSpeed = 10f;
    10.     public float walkAcceleration = 20f;
    11.     public float moveInput;
    12.     public float jumpingPower = 5f;
    13.  
    14.     [Header("Throw Variables")]
    15.     public bool aiming = false;
    16.     public float throwPower = 1;
    17.  
    18.     [Header("Linkings")]
    19.     [SerializeField] private Rigidbody2D rb;
    20.     [SerializeField] private Transform groundCheck;
    21.     [SerializeField] private LayerMask groundLayer;
    22.     [SerializeField] public Transform hand;
    23.     [SerializeField] public Transform spear;
    24.     [SerializeField] public GameObject weapon;
    25.  
    26.     public Spear spearScript;
    27.     public bool spearInHand;
    28.     private float holdDownTimeStart;
    29.     private float aimingTime;
    30.     private float throwForce;
    31.  
    32.     // Vectoren für Handposition
    33.     Vector2 handAimPos;
    34.     Vector2 handIdlePos;
    35.     Vector2 handSwitch;
    36.  
    37.     private void Start()
    38.     {
    39.         handSwitch = new Vector2(-0.5f, 0.4f);
    40.         spearInHand = false;
    41.     }
    42.  
    43.     private void Update()
    44.     {
    45.         if (Input.GetMouseButtonDown(0))
    46.         {
    47.             holdDownTimeStart = Time.time;
    48.             handAimPosition();
    49.         }
    50.  
    51.         if (Input.GetMouseButtonUp(0))
    52.         {
    53.             handIdlePosition();
    54.  
    55.             if (spearInHand)
    56.             {
    57.                 spearInHand = false;
    58.             }
    59.          
    60.         }
    61.  
    62.         moveInput = Input.GetAxisRaw("Horizontal");
    63.  
    64.         if (Input.GetButtonDown("Jump") && IsGrounded())
    65.         {
    66.             rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
    67.         }
    68.  
    69.         if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
    70.         {
    71.             rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
    72.         }
    73.  
    74.         if (Input.GetMouseButtonDown(1))
    75.         {
    76.             Debug.Log("Player says:" + spearScript.spearInAir);
    77.             CreateNewSpear();
    78.             if (!spearInHand && !IsApearInAir())
    79.             {
    80.                 CreateNewSpear();
    81.                 spearInHand = true;
    82.  
    83.             }
    84.  
    85.         }
    86.  
    87.     }
    88.  
    89.     void FixedUpdate()
    90.     {
    91.         rb.velocity = new Vector2(moveInput * movementSpeed, rb.velocity.y);
    92.     }
    93.  
    94.     /*private void Flip()
    95.     {
    96.         if (isFacingRight && moveInput < 0f || !isFacingRight && moveInput > 0f)
    97.         {
    98.             isFacingRight = !isFacingRight;
    99.             Vector2 localScale = transform.localScale;
    100.             localScale.x *= -1f;
    101.             transform.localScale = localScale;
    102.         }
    103.     }*/
    104.     private bool IsGrounded()
    105.     {
    106.         return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
    107.     }
    108.  
    109.     private void handAimPosition()
    110.     {
    111.         handIdlePos = hand.position;
    112.         handAimPos = handIdlePos + handSwitch;
    113.         hand.position = handAimPos;
    114.      
    115.     }
    116.  
    117.     private void handIdlePosition()
    118.     {
    119.         handAimPos = hand.position;
    120.         handIdlePos = handAimPos - handSwitch;
    121.         hand.position = handIdlePos;
    122.     }
    123.  
    124.     public GameObject CreateNewSpear()
    125.     {
    126.         GameObject newSpear = Instantiate(weapon, hand.position, weapon.transform.rotation, gameObject.transform);
    127.         return newSpear;
    128.     }
    129.     private bool IsApearInAir()
    130.     {
    131.         return spearScript.spearInAir;
    132.     }
    133. }
    134.  
    135.  
    136.  

    And....

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Spear : MonoBehaviour
    7. {
    8.     [Header("Linkings")]
    9.     public TestPlayerControl playerScript;
    10.     public Rigidbody2D rbSpear;
    11.     public BoxCollider2D colliderSpear;
    12.  
    13.     [Header("Variables")]
    14.     public float launchForce;
    15.     public bool spearInAir;
    16.  
    17.     Vector2 direction;
    18.  
    19.     // Vectoren für Speerpositionen
    20.     Vector2 spearPosition;
    21.     Vector2 spearAimPos;
    22.     Vector2 handSwitch;
    23.  
    24.     private void Start()
    25.     {
    26.         handSwitch = new Vector2(-0.5f, 0.4f);
    27.     }
    28.  
    29.     void Awake()
    30.     {
    31.         rbSpear.isKinematic = true;
    32.     }
    33.  
    34.  
    35.     void Update()
    36.     {
    37.         Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    38.         spearPosition = transform.position;
    39.         direction = mousePosition - spearPosition;
    40.  
    41.         if (Input.GetMouseButtonDown(0))
    42.         {
    43.  
    44.             spearAimPosition();
    45.  
    46.         }
    47.  
    48.         if (Input.GetMouseButton(0))
    49.         {
    50.             transform.right = direction;
    51.         }
    52.  
    53.         if (Input.GetMouseButtonUp(0))
    54.         {
    55.             Throw();
    56.         }
    57.  
    58.     }
    59.  
    60.     private void FixedUpdate()
    61.     {
    62.  
    63.         if (!playerScript.spearInHand)
    64.         {
    65.             float angle = Mathf.Atan2(rbSpear.velocity.x, rbSpear.velocity.x) * Mathf.Rad2Deg;
    66.             transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    67.         }
    68.  
    69.     }
    70.  
    71.     public void Throw()
    72.     {
    73.         spearInAir = true;
    74.         rbSpear.bodyType = RigidbodyType2D.Dynamic;
    75.         rbSpear.gravityScale = 1;
    76.         rbSpear.AddForce(direction * launchForce, ForceMode2D.Impulse);
    77.     }
    78.  
    79.     private void OnCollisionEnter2D(Collision2D collision)
    80.     {
    81.         GameObject.Destroy(this.gameObject);
    82.     }
    83.  
    84.     private void spearAimPosition()
    85.     {
    86.         spearPosition = transform.position;
    87.         spearAimPos = spearPosition + handSwitch;
    88.         transform.position = spearAimPos;
    89.     }
    90. }
    91.  
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,656
    Yeah, in your Player Script in CreateNewSpear you never actually set spearScript like I had guessed in the previous post.
     
  5. Silasbla

    Silasbla

    Joined:
    Oct 7, 2020
    Posts:
    6
    Oh okay, but I referenced the prefab of Spear. So in my mind every newSpear instantiated will be referenced, cause the prefab is linked to the Player Script?
    upload_2023-4-1_14-49-55.png

    Do you have an idea how to easily fix this issue if im not right? Im quite confused now.
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,656
    That's not how it works, when you Instantiate a new spear, it's going to be a different version of it. When you reference the spearScript in your code, it's still trying to get the data from the prefab, which doesn't exist in your game world. Instead, whenever you create a new spear, you need to point your spearScript variable to that one instead, it won't do it automatically. All you really need to do is edit your CreateNewSpear method to do something like this.

    Code (CSharp):
    1. public GameObject CreateNewSpear()
    2. {
    3.   GameObject newSpear = Instantiate(weapon, hand.position, weapon.transform.rotation, gameObject.transform);
    4.   spearScript = newSpear.GetComponent<Spear>();
    5.   return newSpear;
    6. }
     
    All_American and Silasbla like this.
  7. Silasbla

    Silasbla

    Joined:
    Oct 7, 2020
    Posts:
    6
    You made my saturday a bit nicer today. Got your point an implemented your code. It works now!
    Thank you!