- Home /
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
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.
Thanks. I didn't notice the check. Also, I didn't have time to test your answer. I'll do that today, probably. :)
No problem. :) It's not entirely obvious how this site works when you first show up.
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; }
Deleted my nearly identical answer but yes, you're getting an array, not a single object out of FindGameObjectsWithTag
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... (-_-)
Can you post the exact code you're using? $$anonymous$$ight be able to help you figure it out.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612155744im_/https://answers.unity.com/themes/thub/images/avi.jpg)