x


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

more ▼

asked Jul 26 '11 at 09:14 PM

pcote gravatar image

pcote
16 4 5 6

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.

Jul 27 '11 at 01:10 PM almo

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

Jul 27 '11 at 02:13 PM pcote

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

Jul 27 '11 at 02:20 PM almo
(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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

answered Jul 26 '11 at 09:23 PM

almo gravatar image

almo
1.7k 2 6 18

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

Jul 26 '11 at 09:25 PM Chris D

(untested) code added.

Jul 26 '11 at 09:27 PM almo

Thanks!! :)

I want to place the "for" into a function like "OnMouseDown" and it says that to me:

UnityException: You are not allowed to call this function when declaring a variable. Move 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, Instead 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, instead 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... (-_-)

Jul 27 '11 at 05:20 PM pcote

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

Jul 27 '11 at 05:38 PM Chris D

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.

Jul 27 '11 at 05:46 PM almo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3456
x3327
x215
x54

asked: Jul 26 '11 at 09:14 PM

Seen: 852 times

Last Updated: Jul 27 '11 at 05:58 PM