2
\$\begingroup\$

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!

public void ChangeGreen()
{
     //Turns box collider2d on
     ColorBlockerController blockerScript = GameObject.FindGameObjectWithTag("PinkBlockerTag").GetComponent<ColorBlockerController>();
     blockerScript.PinkBoxOn();
}

public void ChangePink()
{         
     //Turns box collider2d off
     ColorBlockerController blockerScript = GameObject.FindGameObjectWithTag("PinkBlockerTag").GetComponent<ColorBlockerController>();
     blockerScript.PinkBoxOff();
}
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

You can use GameObject.FindGameObjectsWithTag which will return all the similar gameobject and iterate though it

GameObject[] goWithSameTag = GameObject.FindGameObjectsWithTag("TagNAme");
foreach(go in goWithSameTag)
{
    go.GetComponent<BoxCollider2D>().enabled = false;
}

Alternatively you can cache these objects in an array at the start of game so you don't have to run call GameObject.FindGameObjectsWithTag everytime you are enabling/disabling the collider.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks, but when I put in the first line I got "CS0029: Cannot implicitly convert type 'UnityEngine.GameObject[]' to 'UnityEngine.GameObject' " This it what I put- GameObject pBlocker= GameObject.FindGameObjectsWithTag("PinkBlockerTag"); \$\endgroup\$ Commented Jun 6, 2019 at 7:41
  • \$\begingroup\$ @BrandonMccurdy GameObject[] pBlocker= GameObject.FindGameObjectsWithTag("PinkBlockerTag"); I think u may should review csharp a bit. It may save ur time later. \$\endgroup\$
    – AlexWei
    Commented Jun 6, 2019 at 7:55
  • \$\begingroup\$ @BrandonMccurdy This answer contained a couple mistakes. I think I corrected them. Please try again. \$\endgroup\$
    – Philipp
    Commented Jun 6, 2019 at 8:09
2
\$\begingroup\$

FindGameObjectWithTag works, but it is slow when you have a lot of objects.

You may find this event-based solution useful:

public class Whatever {
    private bool state;
    public event Action <bool> onStateChange;

    public bool State {
        get { return state; }
        set {
            state = value;
            onStateChange?.Invoke (value);
        }
    }
}

Store it in a static field or make its members static.

When you spawn a prefab, it first reads State and decided to enable collider or not, and subscribes to event:

whatever.onStateChange += SetColliderState;

In that handler you can do anything you want. For example:

private void SetColliderState (bool state) {
    boxCollider.enabled = state;
}

Don't forget to unsubscribe in OnDestroy! If you don't, the object will not be garbage collected, and its handler will stay and be called forever!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .