FindGameObjectsWithTag enabling scripts...

Hey,

I just wanted to check whether I was going about this in the correct way.

I have a collider.. when a gameobject with a particular tag hits it, I want to do something..all fine, nothing wrong with it. it's what I'm trying to do that's going wrong.

What I want to do is find all game objects with a tag, then enable and disable some scripts within it.

I tried it with

GameObject.FindWithTag("coastforest").GetComponents("SeekSteer").enabled = false;

now this seems to work, but it would just choose one of the gameobjects with that tag, and carry out the change..

So I figured that this would make more sense

GameObject.FindGameObjectsWithTag("coastforest").GetComponents("SeekSteer").enabled = false;

I assumed, in theory that this would look for everything with that tag, then apply the change, but it didn't, I got an error

BCE0019: 'GetComponents' is not a member of '(UnityEngine.GameObject)'.

I know that this is probably not the most efficient way of doing this, as an aside, my hierarchy for these objects is kinda like this

PARENT OBJECT < with main script on CHILD OBJECT CHILD OF CHILD OBJECT < where changes need to be applied

Thanks in advance, Russ.

You must iterate through all the GameObjects returned by FindGameObjectsWithTag and find and disable the component on each GameObject separately

for (o : GameObject in GameObject.FindGameObjectsWithTag("coastforest"))
{
    comp : Component = o.GetComponent("SeekSteer");
    //Only try to disable the component if it exists on the current GameObject
    if (comp != null)
    {
        comp.enabled = false;
    }
}

See the documentation for FindGameObjectsWithTag for another example

Okay, I've gone another way to get the same result...

It's definitely not the most beautiful,elegant, efficient script that there is...but it's a lot more efficient to how I used to have things... My brain is so fried at the moment, that if it works, and works well enough, then it's good enough!

Thanks.

var coastForestFlock1 : GameObject;
var coastForestFlock2 : GameObject;
var coastForestFlock3 : GameObject;
var coastForestFlock4 : GameObject;
var coastForestFlock5 : GameObject;
var coastForestFlock6 : GameObject;
var coastForestFlock7 : GameObject;
var coastForestFlock8 : GameObject;
var coastForestFlock9 : GameObject;
var coastForestFlock10 : GameObject;
var coastForestFlock11 : GameObject;

function OnTriggerEnter (other : Collider)
{
if(other.gameObject.tag == "flocktrigger")

        {
            coastForestFlock1.GetComponent("SeekSteer").enabled = false;
            coastForestFlock1.GetComponent("SmoothFollow2").enabled = true;

            coastForestFlock2.GetComponent("SeekSteer").enabled = false;
            coastForestFlock2.GetComponent("SmoothFollow2").enabled = true;

            coastForestFlock3.GetComponent("SeekSteer").enabled = false;
            coastForestFlock3.GetComponent("SmoothFollow2").enabled = true;

            coastForestFlock4.GetComponent("SeekSteer").enabled = false;
            coastForestFlock4.GetComponent("SmoothFollow2").enabled = true;

            coastForestFlock5.GetComponent("SeekSteer").enabled = false;
            coastForestFlock5.GetComponent("SmoothFollow2").enabled = true;

            coastForestFlock6.GetComponent("SeekSteer").enabled = false;
            coastForestFlock6.GetComponent("SmoothFollow2").enabled = true;

            coastForestFlock7.GetComponent("SeekSteer").enabled = false;
            coastForestFlock7.GetComponent("SmoothFollow2").enabled = true;

            coastForestFlock8.GetComponent("SeekSteer").enabled = false;
            coastForestFlock8.GetComponent("SmoothFollow2").enabled = true;

            coastForestFlock9.GetComponent("SeekSteer").enabled = false;
            coastForestFlock9.GetComponent("SmoothFollow2").enabled = true;

            coastForestFlock10.GetComponent("SeekSteer").enabled = false;
            coastForestFlock10.GetComponent("SmoothFollow2").enabled = true;

            coastForestFlock11.GetComponent("SeekSteer").enabled = false;
            coastForestFlock11.GetComponent("SmoothFollow2").enabled = true;
        }

}