x


How to find all objects with an tag and change an value in all of them?

I have 7 separate objects on my scene with tag "Enemy" and what i want to do now is to change value "RaceBeginner" from false to true in script called "EnemyShipAI" on every object with tag "Enemy".

I found something like this but i have no idea how to use it to change those values:

GameObject.FindGameObjectsWithTag("EnemyAI").GetComponent("EnemyShipAI").RaceBeginnerEnemy = true;

more ▼

asked Dec 29 '11 at 03:19 PM

zero3growlithe gravatar image

zero3growlithe
67 17 23 26

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
var enemies = GameObject.FindGameObjectsWithTag("EnemyAI");
for (enemy in enemies) enemy.GetComponent(EnemyShipAI).RaceBeginnerEnemy = true;

It's bad practice to use quotes with GetComponent. Also it would be easier if you follow the convention of using lowercase for variable names and uppercase for class and function names.

more ▼

answered Dec 29 '11 at 03:34 PM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

(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:

x3316
x379
x58
x40
x33

asked: Dec 29 '11 at 03:19 PM

Seen: 542 times

Last Updated: Dec 29 '11 at 03:34 PM