Search Unity

How to disable box collider2D on All Prefabs

Discussion in 'Prefabs' started by RetroboltGames, Jun 6, 2019.

  1. RetroboltGames

    RetroboltGames

    Joined:
    Oct 26, 2018
    Posts:
    11
    I made a prefab that when hitting one object will disable it's box collider2d and enable it after hitting a different object.

    It works find when there's one prefab, but when there's move, it only works on a single instance of the prefab and non of the others.

    How do I set it to disable every box collider2d on every prefab object of that type? Thank you!

    Code (CSharp):
    1. public void ChangeGreen()
    2.      {
    3.         //Turns box collider2d on
    4.          ColorBlockerController blockerScript = GameObject.FindGameObjectWithTag("PinkBlockerTag").GetComponent<ColorBlockerController>();
    5.          blockerScript.PinkBoxOn();
    6.      }
    7. public void ChangePink()
    8.      {
    9.        
    10.          //Turns box collider2d off
    11.          ColorBlockerController blockerScript = GameObject.FindGameObjectWithTag("PinkBlockerTag").GetComponent<ColorBlockerController>();
    12.          blockerScript.PinkBoxOff();
    13.      }
     
  2. eudendeew

    eudendeew

    Joined:
    Feb 5, 2017
    Posts:
    4
    You will have to do that with a list or array of instances.

    As you said, your scene has instances of the prefab not the prefabs itself. Modifying one instance won't affect another nor the original prefab.

    A better idea would be having a public array, then drag drop all the references in the inspector.
    Using Array<ColorBlockerController> would easier since you can use .Add() to add a new blocker controller. You can even let them add themselves at Start to the list.
    Also don't forget that deactivating the collider will make trigger and collision detection impossible on that object.

    But... a way to achieve what you want would be like:
    Code (CSharp):
    1.  
    2. // On the main controller:
    3. private ColorBlockerController[] allBlockers;
    4.  
    5. void Start()
    6. {
    7.     GameObject[] blockersObjs = GameObject.FindGameObjectsWithTag("PinkBlockerTag"); // Notice the s in "ObjectS"
    8.     allBlockers = new ColorBlockerController[blockersObjs.Lenght]; // Init array
    9.     for (i = 0; i < blockersObjs.Lenght; i++) { // Fill up array
    10.         // Beware that if an object with tag "PinkBlockerTag" doesn't have ColorBlockerController, you will fill your array with null. An you'll get Null Reference in ChangeGreen()
    11.         allBlockers[i] = blockersObjs[i].GetComponent<ColorBlockerController>();
    12.     }
    13. }
    14.  
    15. public void ChangeGreen()
    16. {
    17.     foreach(var blocker in allBlockers)
    18.     { // Change all referenced ColorBlockerController in allBlockers array
    19.         blocker.PinkBoxOn();
    20.     }
    21. }
    22.  
    Cheers.
     
    RetroboltGames likes this.
  3. RetroboltGames

    RetroboltGames

    Joined:
    Oct 26, 2018
    Posts:
    11
    Thanks, that worked!