Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 pcote · Jul 26, 2011 at 09:14 PM · javascriptdisablefindgameobjectswithtag

How to disable scripts inside objects found with tag

Hello,

I'm trying to find objects with a certain tag then disable one specific script inside those objects but cannot manage to make it work so far.

Here is what I was trying lately:

 var blockfall : gameObject.FindGameObjectsWithTag("Block").GetComponent(BlockFall).
 enabled;
 blockfall = false;

Can someone explain me what is going on here and why I cannot do such a thing ?

Thank you very much! :)

Phil

Comment
Add comment · Show 3
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 almo · Jul 27, 2011 at 01:10 PM 0
Share

You have 3 open questions here, two of which have answers. If you get a useful answer, you're supposed to "accept" it by clicking the checkmark by the answer that helped.

avatar image pcote · Jul 27, 2011 at 02:13 PM 0
Share

Thanks. I didn't notice the check. Also, I didn't have time to test your answer. I'll do that today, probably. :)

avatar image almo · Jul 27, 2011 at 02:20 PM 0
Share

No problem. :) It's not entirely obvious how this site works when you first show up.

1 Reply

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

Answer by almo · Jul 26, 2011 at 09:23 PM

First, FindGameObjectsWithTag returns a list of objects.

Second, you need to loop through all those objects.

Third, you need to get the BlockFall component from each object and set its enabled property to false.

var blocks = GameObject.FindGameObjectsWithTag ("Block");

for (var block in blocks) { var blockfall : BlockFall; blockfall = block.GetComponent(BlockFall); blockfall.enabled = false; }

Comment
Add comment · Show 7 · 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 Chris D · Jul 26, 2011 at 09:25 PM 1
Share

Deleted my nearly identical answer but yes, you're getting an array, not a single object out of FindGameObjectsWithTag

avatar image almo · Jul 26, 2011 at 09:27 PM 0
Share

(untested) code added.

avatar image pcote · Jul 27, 2011 at 05:20 PM 0
Share

Thanks!! :)

I want to place the "for" into a function like "On$$anonymous$$ouseDown" and it says that to me:

UnityException: You are not allowed to call this function when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration. If you are using C# don't use this function in the constructor or field initializers, Ins$$anonymous$$d move initialization to the Awake or Start function. BlockDrag..ctor () (at Assets/BlockDrag.js:5)

ArgumentException: FindGameObjectsWithTag can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function. BlockDrag..ctor () (at Assets/BlockDrag.js:5)

I'll try something else and tell you how it goes.

UPDATE:

Well, is there any way of disabling multiple components at once without using the "FindGameObjectsWithTag" ? I can't seem to get anything working while using a function that triggers on mouse button down... (-_-)

avatar image Chris D · Jul 27, 2011 at 05:38 PM 0
Share

Can you post the exact code you're using? $$anonymous$$ight be able to help you figure it out.

avatar image almo · Jul 27, 2011 at 05:46 PM 0
Share

What you should do is get this working so you can turn them off, not using mousedown.

Triggering a function on mousedown would then be another question you would ask. Break your tasks into bits, or Unity Answers won't be able to help you.

Show more comments

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I create a simply disable script for enemy AI once it completes its waypoints? 2 Answers

Disable script from code 5 Answers

Script to disable MouseLook? 2 Answers

What should I do? Disable the AI or have the AI simply be recylced and reused? 1 Answer

How do I resize the image on a GUILayout? 1 Answer


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