• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by therussmorris · Apr 06, 2010 at 12:38 PM · gameobject

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KvanteTore · Apr 06, 2010 at 01:07 PM 0
Share

It would be nice if you reformat the question with each code-line indented by four spaces. It would be easier to read that way. :)

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by KvanteTore · Apr 06, 2010 at 12:47 PM

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image therussmorris · Apr 06, 2010 at 01:37 PM 0
Share

okay, my brain may be fried, because I'm making absolutely no sense out of this.

I've not used for before, and I'm having difficulty understanding what the o : is for?

avatar image KvanteTore · Apr 06, 2010 at 02:25 PM 0
Share

o is the loop variable. o : GameObject, means loop variable named o of type GameObject. If you have never used a loop before, I would recommend you to pick up a basic program$$anonymous$$g tutorial :). There are some basic scripting tutorials on http://www.unity-tutorials.com/ (some free, others at a premium), but I have no idea about the quality of these tutorials.

avatar image
0

Answer by therussmorris · Apr 06, 2010 at 02:21 PM

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;
     }

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KvanteTore · Apr 06, 2010 at 02:28 PM 1
Share

I really recommend you to look into how loops work http://en.wikipedia.org/wiki/Control_flow#Loops.

avatar image therussmorris · Apr 06, 2010 at 02:32 PM 0
Share

Hey,

yeh I know I can go about it in a way that will quicker/easier. but at this moment in time, I can get things working. I'd rather get things working again, then go back when I've got a bit more time/a little less stress. There are other things that I need to test that rely on this working.

Thanks very much though.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Best practice for copying Components from one GameObject to another? 3 Answers

Setting unique data on a gameobject? 1 Answer

melee system script problem, what did i do wrong or what to do next? 0 Answers

C# Adding Components From Other Gameobjects 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges