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

GameObjects Lists in BoxCollider2D?

Discussion in '2D' started by RuneShiStorm, Oct 27, 2022.

  1. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Hi all!
    I have asked the community about lists for as long as I remember and never ever have I fully understood how they work. I end up doing insanely inconvenient NOOB scripts to make it work.
    So, I'm here to ask the community to help me write a script so I can see it in its fully glory, and I'm sure other noobs in the future can use it for their own games.

    As you can see in the Image, I want to enter a house in 2D, when entering the house (BoxCollider2D) I want some objects to turn off, others turn on. These objects differ depending on the houses. Thus, I want lists that I can fill with objects that will turn off or turn on. Does that make sense? (Yes, I know I can keep all the objects hidden behind the House Wall. But this Image is just a visual example).



    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ActivationScript : MonoBehaviour
    7. {
    8.  
    9.     public List<GameObject> onObjects;
    10.     public List<GameObject> offObjects;
    11.  
    12.     void OnTriggerEnter2D(Collider2D other)
    13.     {
    14.         if (other.gameObject.tag == "Player")
    15.         {
    16.           ????????????????
    17.         }
    18.     }
    19.     private void OnTriggerExit2D(Collider2D other)
    20.     {
    21.         if (other.gameObject.tag == "Player")
    22.         {
    23.           ????????????????
    24.         }
    25.     }
    26. }
    27.  
    Thanks in advance and I would love to solve this script once and for all with you all!
     

    Attached Files:

    Last edited: Oct 27, 2022
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,523
    This isn't really a post for 2D, it's seems like a post for the Scripting forum because, honestly, the above isn't even valid C# code and won't even compile but you are asking about specific mechanics and not the fact that the code won't even compile which is a little confusing.

    I can only assume you mean a Generic List i.e. List<T> so instead of:
    Code (CSharp):
    1. public List GameObject onObjects;
    you mean something like:
    Code (CSharp):
    1. public List<GameObject> onObjects;
    I understand your question but it seems you need to first address the C# usage issue?

    I'll hold off moving your post to scripting forum though.
     
    RuneShiStorm likes this.
  3. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264

    yes, You're right. The code isn't working cuz I wrote " Strings" in my script and then I though, "Whoa... My question mention lists and It has strings in the script... " So i just changed the script to show my point. But I should rewrite it. Pls move the thread if you want! I will Edit the script so it complies at least!
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,523
    To answer your question given that you've put all the appropriate GameObject to show/hide in those list then you'd do something like this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class ActivationScript : MonoBehaviour
    5. {
    6.     public List<GameObject> onObjects;
    7.     public List<GameObject> offObjects;
    8.  
    9.     void OnTriggerEnter2D(Collider2D other)
    10.     {
    11.         if (other.gameObject.tag == "Player")
    12.         {
    13.             SetEnterExitVisibility(true);
    14.         }
    15.     }
    16.  
    17.     private void OnTriggerExit2D(Collider2D other)
    18.     {
    19.         if (other.gameObject.tag == "Player")
    20.         {
    21.           SetEnterExitVisibility(false);
    22.         }
    23.     }
    24.  
    25.     private void SetEnterExitVisibility(bool isEntering)
    26.     {
    27.         foreach(var obj in onObjects)
    28.             obj.SetActive(isEntering);
    29.  
    30.         foreach(var obj in offObjects)
    31.             obj.SetActive(!isEntering);
    32.     }
    33. }
    You're better off though simply making it so that the above trigger only interacts with the "Player" layer. Using tags isn't something you have to do and should avoid unless you have to use them.
     
    RuneShiStorm likes this.
  5. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Why is it bad to use Tags? CPU power?
    It works now thanks to your script, thanks!
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,523
    Nothing intrinsically bad about them but they're not used by the physics engine at all.

    The physics engine provides the low-level and fast ability for you to select which layers can contact other layers. If you use this, not only does the physics engine not need to calculate unnecessary layer/layer interactions but you can also simply check which layer you've got. This is shown in all the basic physics tutorials though and it's what the Layer Collision Matrix provides.

    What we see though is devs allowing everything to contact everything and then simply deciding if it's something they're interested in with a tag, mostly with triggers. It works but it's also wasted work.

    Also, writing hardcoded strings all over your code isn't very scalable and is pretty fragile to change.
     
    Chubzdoomer likes this.