# Get all objects with tag script

**URL:** https://discussions.unity.com/t/get-all-objects-with-tag-script/479608
**Category:** Scripting
**Tags:** Scripting
**Created:** [June 24, 2012, 6:55pm UTC](https://discussions.unity.com/t/get-all-objects-with-tag-script/479608 "2012-06-24T18:55:16Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![anon\_97610600](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@anon\_97610600](https://discussions.unity.com/u/anon_97610600)
#### Post date: [June 24, 2012, 6:55pm UTC](https://discussions.unity.com/t/get-all-objects-with-tag-script/479608/1 "2012-06-24T18:55:16Z")

</div>

How could i get all objects with a specific tag and then set a variable in a script they have?

---

<div class="post-metadata">

### Author: ![Morning](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@Morning](https://discussions.unity.com/u/Morning)
#### Post date: [June 24, 2012, 7:02pm UTC](https://discussions.unity.com/t/get-all-objects-with-tag-script/479608/2 "2012-06-24T19:02:21Z")

</div>

> **[Unity - Scripting API: GameObject.FindGameObjectsWithTag](https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html)**

Has both examples you want.

---

<div class="post-metadata">

### Author: ![anon\_97610600](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@anon\_97610600](https://discussions.unity.com/u/anon_97610600)
#### Post date: [June 24, 2012, 7:11pm UTC](https://discussions.unity.com/t/get-all-objects-with-tag-script/479608/3 "2012-06-24T19:11:53Z")

</div>

Sorry frazed what i said badly. How would I get a script each one of the objects with the specific tag.

---

<div class="post-metadata">

### Author: ![Morning](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@Morning](https://discussions.unity.com/u/Morning)
#### Post date: [June 24, 2012, 7:13pm UTC](https://discussions.unity.com/t/get-all-objects-with-tag-script/479608/4 "2012-06-24T19:13:24Z")

</div>

ScriptName src = FoundObject.GetComponent();

---

<div class="post-metadata">

### Author: ![anon\_97610600](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@anon\_97610600](https://discussions.unity.com/u/anon_97610600)
#### Post date: [June 24, 2012, 7:17pm UTC](https://discussions.unity.com/t/get-all-objects-with-tag-script/479608/5 "2012-06-24T19:17:28Z")

</div>

Scripts Variable:)

---

<div class="post-metadata">

### Author: ![Morning](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@Morning](https://discussions.unity.com/u/Morning)
#### Post date: [June 24, 2012, 7:21pm UTC](https://discussions.unity.com/t/get-all-objects-with-tag-script/479608/6 "2012-06-24T19:21:20Z")

</div>

src.PublicVarName

---

<div class="post-metadata">

### Author: ![anon\_97610600](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@anon\_97610600](https://discussions.unity.com/u/anon_97610600)
#### Post date: [June 24, 2012, 7:32pm UTC](https://discussions.unity.com/t/get-all-objects-with-tag-script/479608/7 "2012-06-24T19:32:58Z")

</div>

Ok im sorry i dont understand how that would go with what im saying. To find all objects with a specific tag then go into there script and change a variable or check it.

---

<div class="post-metadata">

### Author: ![Morning](https://avatars.discourse-cdn.com/v4/letter/m/e480ec/32.png) [@Morning](https://discussions.unity.com/u/Morning)
#### Post date: [June 24, 2012, 7:37pm UTC](https://discussions.unity.com/t/get-all-objects-with-tag-script/479608/8 "2012-06-24T19:37:36Z")

</div>

```auto
void Start
	{
	GameObject[] objs = GameObject.FindGameObjectsWithTag("Enemy");
	foreach(GameObject g in objs)
	{
	CustomScript cs = g.GetComponent<CustomScript>();
	cs.DoCustomVar = true;
	}
}

```

I could’ve missed something but overall this should do.  
It will throw an error if the object with that tag doesn’t have the script but I’ll leave error handling to you.

---

<div class="post-metadata">

### Author: ![anon\_97610600](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@anon\_97610600](https://discussions.unity.com/u/anon_97610600)
#### Post date: [June 24, 2012, 7:45pm UTC](https://discussions.unity.com/t/get-all-objects-with-tag-script/479608/9 "2012-06-24T19:45:34Z")

</div>

Could you make that javascript? Thanks

---

<div class="post-metadata">

### Author: ![anon\_97610600](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@anon\_97610600](https://discussions.unity.com/u/anon_97610600)
#### Post date: [June 24, 2012, 8:43pm UTC](https://discussions.unity.com/t/get-all-objects-with-tag-script/479608/10 "2012-06-24T20:43:25Z")

</div>

Please?

---

<div class="post-metadata">

### Author: ![anon\_97610600](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@anon\_97610600](https://discussions.unity.com/u/anon_97610600)
#### Post date: [June 24, 2012, 9:45pm UTC](https://discussions.unity.com/t/get-all-objects-with-tag-script/479608/11 "2012-06-24T21:45:28Z")

</div>

Ok i got this togethor but how do i check the variable using a static variable or an other way?(im trying to check if all humans have zero health)

```auto
function chek(){

target = GameObject.FindGameObjectsWithTag("human");
 for(GameObject in target)
    {
    if(){
	
	}
    }

}

```

---

<div class="post-metadata">

### Author: ![popmog78](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/popmog78/32/507757_2.png) [@popmog78](https://discussions.unity.com/u/popmog78)
#### Post date: [June 24, 2012, 9:52pm UTC](https://discussions.unity.com/t/get-all-objects-with-tag-script/479608/12 "2012-06-24T21:52:18Z")

</div>

Do you want to try OnCollision enter to check the Game Object?

```auto
function OnTriggerEnter(Collider otherObject){
     
if(otherObject.tag == "human")
        {
    		
       Do something you want;
        }

     
    }

```

---

<div class="post-metadata">

### Author: ![anon\_97610600](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@anon\_97610600](https://discussions.unity.com/u/anon_97610600)
#### Post date: [June 24, 2012, 9:54pm UTC](https://discussions.unity.com/t/get-all-objects-with-tag-script/479608/13 "2012-06-24T21:54:16Z")

</div>

No im trying to get every game object with the tag human and check there health value and if there all = 0 then do something.

```auto
function chek(){

target = GameObject.FindGameObjectsWithTag("human");
 for(GameObject in target){
 
    if(target.GetComponent(HumanNormalScript).actionpoints <= 0){
	print("hey");
	}
	
    }

}

```
