Search Unity

Question about using box collider on treasure chest

Discussion in 'Getting Started' started by NicexGuy, Jan 3, 2023.

  1. NicexGuy

    NicexGuy

    Joined:
    Dec 29, 2022
    Posts:
    2
    hi, i am a beginner of unity and C#, i watched some tutorials and my mind come across this question when i am making my first game recently.
    from the tutorial, it teaches me to make a script for the treasure chest, and in the update, using a for loop to look for colliding object, if it is a player, then open.
    i wonder if this is a good approach to for all the chest each using a for loop to look for the player to interact, would it cost the user a lot of resource when i have 1000 chests and many other object working the same way?

    the reason i ask this is that if it would cause a problem, i should better change the perspective to the player, using the player's collider to look for object's tag. (that will take some time as chest is not the only thing that need mending)
    many thanks in advance.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    This is indeed a bad approach. The typical approach for something like this is not to use Update. You would normally use a trigger collider and OnTriggerEnter:
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html