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

Why does my function get called for every object?

Discussion in 'Scripting' started by Sprinklotad, Jun 13, 2020.

  1. Sprinklotad

    Sprinklotad

    Joined:
    Jun 11, 2020
    Posts:
    4
    Hi, I'm new to unity. I have a script called "PickUp" which I've attached to items that are able to be picked up. I've got it working, but I have a question.

    The logic is basically:
    onTriggerEnter() -> allow pickup
    onTriggerExit() -> disallow pickup
    update() -> if pickup is allowed and user is pressing E, call pickItem()
    pickItem() -> For now, log the value of the objects

    I've attached it here:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PickUp : MonoBehaviour
    6. {
    7.  
    8.     public int value;
    9.  
    10.     private bool inRange;
    11.  
    12.     private void Start()
    13.     {
    14.      }
    15.  
    16. // checks that the player is in range
    17.     private void OnTriggerEnter2D(Collider2D other)
    18.     {
    19.         if (other.CompareTag("Player"))
    20.         {
    21.             inRange = true;
    22.             Debug.Log("pickup allowed.");
    23.  
    24.         }
    25.     }
    26.  
    27. // when the player is no longer in range, the object cannot be picked up
    28.     private void OnTriggerExit2D(Collider2D other)
    29.     {
    30.         if (other.CompareTag("Player"))
    31.         {
    32.             inRange = false;
    33.             Debug.Log("pickup no longer allowed.");
    34.  
    35.         }
    36.     }
    37.  
    38.  
    39.     private void PickItem()
    40.     {
    41.         Debug.Log("this item is valued at " + value);
    42.  
    43.     }
    44.  
    45.     private void Update()
    46.     {
    47.         if (inRange = true && Input.GetKeyDown(KeyCode.E))
    48.         {
    49.             PickItem();
    50.         }
    51.  
    52.     }
    53. }
    At the moment, I have 4 items with the PickUp script.
    However, when I go to an object and press E, I get the value of all 4 objects instead of the one my player is touching.

    upload_2020-6-12_22-41-12.png

    I also tried Debug.log(gameObject), but it ended up logging each one as well.
    upload_2020-6-12_22-49-59.png

    I finally got it to work by moving my logic to onTriggerStay( ).
    But I still want to know if there's a proper way to call a function for a single gameobject.
     
    Last edited: Jun 13, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    This line. The = operator is assignment. The == operator is testing (boolean).

    You are assigning (and implicitly testing) the value that evalutes from
    true && keydown
    in every instance.

    You should use == but you should group them by parentheses.

    Code (csharp):
    1. if ((inRange == true) && Inputetc)
    In fact, you don't even really need the
    true
    , you can just use the
    (inRange && Inputetc)
    expression for your if condition.

    But keeping the true is not bad, it's actually a style preference for some, although I don't care for it much.

    However, always keep the parens. :)
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    Your problem had nothing to do with OnTriggerEnter, but rather your Update check itself.

    inRange = true is an assignment call. It means, set the value of inRange to true.

    inRange == true or even just putting inRange means to compare the value to true.

    Edit: @Kurt-Dekker beat me to it. lol.
     
  4. Sprinklotad

    Sprinklotad

    Joined:
    Jun 11, 2020
    Posts:
    4
    @Kurt-Dekker @Brathnann gosh, thank you! I was losing my mind. Just tested it and it works! And dang yeah, since it is a boolean I could have done the check using the property name :eek:
     
    Kurt-Dekker likes this.