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.

Question How to store multiple colliders in OnTriggerStay() as GameObjects?

Discussion in 'Scripting' started by karsten-heim, Nov 27, 2022.

  1. karsten-heim

    karsten-heim

    Joined:
    May 16, 2022
    Posts:
    52
    I have a script where I have a sphere collider that detects objects within it that can be picked up, but I don't know how to store more than one of the colliders as a GameObject, which I will then subtract it's position from the players to see if it is the closest object to the player, and then assign that to a GameObject variable. Here is the code that I have so far:
    Code (CSharp):
    1. public void OnTriggerStay(Collider other)
    2.     {
    3.         closeobj = other.gameObject.GetComponent<pickupscript>();
    4.         if(other.gameObject.tag == "AK" && closeobj.equipped == false )
    5.         {
    6.             Debug.Log(other.gameObject.name);
    7.         }
    8.      
    9.     }
    The Debug.Log gives me the names of the items in range, making me think storing these is possible, but I do not know how to assign them when all I appear to have to reference them is "other.gameObject"
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    31,358
    You can drag colliders into public fields on your script, then compare them to the one passed into the above function to decide what to do.

    Alternately you can make child objects and have different colliders on different layers, perhaps adjusting the physics collision matrix to keep only the ones you want colliding.
     
  3. karsten-heim

    karsten-heim

    Joined:
    May 16, 2022
    Posts:
    52
    I don't think that the first one would work for what I am doing, because I have a significant amount of items, and you may have to elaborate more on the second one or me to understand it.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    31,358
    Layer properties can be used to control the physics system behavior.

    Layers can only be set on a GameObject basis, not on individual colliders on a single GameObject.
     
  5. karsten-heim

    karsten-heim

    Joined:
    May 16, 2022
    Posts:
    52
    Would that work if I am handling multiple unique GameObjects though?
    using layers seems like it would only differentiate items from the ground or walls or something
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    31,358
    There are only so many layers and broadly they are used for categories of things.

    Otherwise, put a MonoBehaviour on each object and use GetComponent<T>() (sort of like you're doing above on line 3) to get that MonoBehaviour instance and read the unique-ness out yourself.

    Alternately, you can use interfaces to do the same thing more generically.

    Using Interfaces in Unity3D:

    https://forum.unity.com/threads/how...rereceiver-error-message.920801/#post-6028457

    https://forum.unity.com/threads/manager-classes.965609/#post-6286820

    Check Youtube for other tutorials about interfaces and working in Unity3D. It's a pretty powerful combination.
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,026
    What're you actually trying to achieve? "Store it as a GameObject" variable generally isn't what you want, you want to equip the item or something like that.

    If you just want the closest one in a field, you can take the object that you find in OnTriggerStay, get it's distance to the player, and compare that to the current distance between the thing already in that field and the player. Then you have a field with the closest thing, if that's all you want.
     
  8. karsten-heim

    karsten-heim

    Joined:
    May 16, 2022
    Posts:
    52
    Yes, I would like to get only the closest object, but I do not know how to reference it if there is more than one object in the trigger so that I may store the closest object as a GameObject. I tried using an array to store the GameObjects, but then realized that I have no idea how to use an array.