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. Dismiss Notice

Method Problem

Discussion in 'Scripting' started by Moddwyn, Aug 2, 2016.

  1. Moddwyn

    Moddwyn

    Joined:
    Jan 27, 2016
    Posts:
    49
    Code (csharp):
    1.  
    2.  
    3. public GameObject[] items;
    4. public GameObject pickup;
    5.  
    6.  
    7. void Update(){
    8.         if(PickedUpItem()){
    9.               Debug.Log("Yay!");
    10.         }
    11. }
    12.  
    13. public bool PickedUpItem(){
    14.         foreach(GameObject i in items){
    15.               if (pickup.GetComponent <Collider2D>().IsTouching (i.GetComponent <Collider2D>())){
    16.                     return true;
    17.               }
    18.         }
    19.         return false;
    20. }
    21.  

    what suppose to happen is when i touch one of the gameobjects in items, im gonna return true in the PickupUpItem(). But it wont return true (say "Yay") even though i've touched one of the items. What could be causing the problem?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    I'm not sure why you are checking in update if you've picked up an item. You can use something like OnTriggerEnter or OnCollisionEnter or something much better to trigger a "Hey I've been picked up" instead of asking every pickup if it's been touched.

    The other thing is, maybe when you're touching a physics update hasnt' taken place.
    https://docs.unity3d.com/ScriptReference/Collider2D.IsTouching.html
     
  3. Moddwyn

    Moddwyn

    Joined:
    Jan 27, 2016
    Posts:
    49
    I'm making a plugin. I wanted a function that checks if i picked up an item.
     
  4. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,109
    right, and like he said.. don't do it in update, have it execute on a trigger event instead and save some CPU cycles.
     
    TaleOf4Gamers likes this.
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    I'm not sure why making a plugin would be any different.
    If you have a character that is picking up stuff, a player script on that character would be better with collision or trigger checks.
     
    TaleOf4Gamers likes this.
  6. Moddwyn

    Moddwyn

    Joined:
    Jan 27, 2016
    Posts:
    49
    im gonna be posting this to the asset sotre. How if people wanna check if the player pickups an item, mybe like give xp or something.

    its gonna be easy by just doing it like this:
    Code (csharp):
    1.  
    2. if(itemPickupScript.PickedUpItem == true){
    3.      GiveExp();
    4. }
    5.  
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    It's not a good way to do it, and I would pass on an asset like this. It'd be easy to have people setup their pickups with a tag to make it a pickup item, that way you just check the objects tag with OnTriggerEnter or OnCollisionEnter.

    Even if you don't want to do that, if you have a list of pickup items and you just want to check if that list contains an item the player char collides with, you'd still trigger that check in OnTriggerEnter or OnCollisionEnter.

    Either method is better than looping through an array multiple times through an update call.

    Think of it this way. If you were in a building full of 100 people and I told you to go ask each of them if they were hungry and you had to do this over and over and over....
    or, you could wait for a person to come to you and tell you they are hungry. Which is better?