Skip to main content
grammar and punctuation; removed blacklisted engine tag
Source Link
Gnemlock
  • 5.3k
  • 5
  • 30
  • 60

Find How do I find all Game Objectsgame objects with an Input stringparticular name (not Tag)?

I'm trying to mimic FindGameObjectsWithTag with FindGameObjectswithNameFindGameObjectsWithName. I know using tags is faster, but I'm trying create a way to search for objects I spawn with a child object I name on instantiation through the inspector.

My goal is create multiple enemy spawners. My current spawner has a drag and drop variable for enemyprefabsenemy prefabs. It looks for enemies tagged "ReSpawn", then checks if that length is below (x)a value, to start spawning more. I would like to upgrade my current single spawner to still check for enemies named respawn"ReSpawn", but then check if those respawnrespawns have a child object named "blank" (an input stringa name I can specifyspecified in the editor).

This This way, I can set the enemy spawners anywhere (neare.g. near water, tall grass, forests, etc), and when I defeat one, the specific spawner will instantiate one of its opponentopponents for that area.

 

Here is my code, so far:

var ThisSpot:String; var Oppos : GameObject[]; var OppoCount : GameObject[]; var HowMany = 3; var spawnSizeArea = 25; function Start() { StartCoroutine ("Spawn"); } function Update () { OppoCount = GameObject.FindGameObjectsWithTag("Respawn"); // OppoCount = GameObject.FindWithName(ThisSpot); This is what I am trying to achieve if(OppoCount.length < HowMany) { StartCoroutine ("Spawn"); } } function Spawn() { var length = Oppos.length-1; var index = Mathf.Round(length*UnityEngine.Random.value); curSpawn = Oppos[index]; var xz = Random.insideUnitCircle * spawnSizeArea; var newPosition = Vector3(xz.x,0,xz.x)+transform.position; yield WaitForSeconds(Random.Range(70,120)); Instantiate(curSpawn, newPosition, transform.rotation); // Then I could use these 2 lines of code below to use multiple spawners //var newOp : GameObject = Instantiate(curSpawn, newPosition, transform.rotation); //newOp.transform.Find("fighter").gameObject.name=ThisSpot; StopCoroutine("Spawn"); } 

EDIT: the solution. Instead of looking for gameobject names/tags, just keep track of a list

 **function Update () { for (var i =0; i < OppoCount.length ; i ++) if(OppoCount.length > 0){ if (OppoCount[i].gameObject == null) OppoCount.RemoveAt(i); } if(OppoCount.length < HowMany){ StartCoroutine ("Spawn"); } } function Spawn(){ var length = Oppos.length-1; var index = Mathf.Round(length*UnityEngine.Random.value); curSpawn = Oppos[index]; //var newPosition = Random.insideUnitCircle* spawnSizeArea;  var xz = Random.insideUnitCircle * spawnSizeArea; var newPosition = Vector3(xz.x,0,xz.x)+transform.position; //yield WaitForSeconds(Random.Range(70,120)); yield WaitForSeconds(Random.Range(2,4)); //Instantiate(curSpawn, newPosition, transform.rotation); var newOp : GameObject = Instantiate(curSpawn, newPosition, transform.rotation); //newOp.transform.Find("fighter").gameObject.active = false; OppoCount.Push(newOp); StopCoroutine("Spawn");  }** 

Find all Game Objects with an Input string name (not Tag)

I'm trying to mimic FindGameObjectsWithTag with FindGameObjectswithName. I know using tags is faster, but I'm trying create a way to search for objects I spawn with a child object I name on instantiation through the inspector.

My goal is create multiple enemy spawners. My current spawner has a drag and drop variable for enemyprefabs. It looks for enemies tagged "ReSpawn", then checks if that length is below (x) to start spawning more. I would like to upgrade my current single spawner to still check for enemies named respawn, but then check if those respawn have a child object named "blank" (an input string name I can specify in the editor).

This way, I can set the enemy spawners anywhere (near water, tall grass, forests, etc), and when I defeat one, the specific spawner will instantiate one of its opponent for that area.

Here is my code

var ThisSpot:String; var Oppos : GameObject[]; var OppoCount : GameObject[]; var HowMany = 3; var spawnSizeArea = 25; function Start() { StartCoroutine ("Spawn"); } function Update () { OppoCount = GameObject.FindGameObjectsWithTag("Respawn"); // OppoCount = GameObject.FindWithName(ThisSpot); This is what I am trying to achieve if(OppoCount.length < HowMany) { StartCoroutine ("Spawn"); } } function Spawn() { var length = Oppos.length-1; var index = Mathf.Round(length*UnityEngine.Random.value); curSpawn = Oppos[index]; var xz = Random.insideUnitCircle * spawnSizeArea; var newPosition = Vector3(xz.x,0,xz.x)+transform.position; yield WaitForSeconds(Random.Range(70,120)); Instantiate(curSpawn, newPosition, transform.rotation); // Then I could use these 2 lines of code below to use multiple spawners //var newOp : GameObject = Instantiate(curSpawn, newPosition, transform.rotation); //newOp.transform.Find("fighter").gameObject.name=ThisSpot; StopCoroutine("Spawn"); } 

EDIT: the solution. Instead of looking for gameobject names/tags, just keep track of a list

 **function Update () { for (var i =0; i < OppoCount.length ; i ++) if(OppoCount.length > 0){ if (OppoCount[i].gameObject == null) OppoCount.RemoveAt(i); } if(OppoCount.length < HowMany){ StartCoroutine ("Spawn"); } } function Spawn(){ var length = Oppos.length-1; var index = Mathf.Round(length*UnityEngine.Random.value); curSpawn = Oppos[index]; //var newPosition = Random.insideUnitCircle* spawnSizeArea;  var xz = Random.insideUnitCircle * spawnSizeArea; var newPosition = Vector3(xz.x,0,xz.x)+transform.position; //yield WaitForSeconds(Random.Range(70,120)); yield WaitForSeconds(Random.Range(2,4)); //Instantiate(curSpawn, newPosition, transform.rotation); var newOp : GameObject = Instantiate(curSpawn, newPosition, transform.rotation); //newOp.transform.Find("fighter").gameObject.active = false; OppoCount.Push(newOp); StopCoroutine("Spawn");  }** 

How do I find all game objects with particular name?

I'm trying to mimic FindGameObjectsWithTag with FindGameObjectsWithName. I know using tags is faster, but I'm trying create a way to search for objects I spawn with a child object I name on instantiation through the inspector.

My goal is create multiple enemy spawners. My current spawner has a drag and drop variable for enemy prefabs. It looks for enemies tagged "ReSpawn", then checks if that length is below a value, to start spawning more. I would like to upgrade my current single spawner to still check for enemies named "ReSpawn", but then check if those respawns have a child object a name specified in the editor. This way, I can set the enemy spawners anywhere (e.g. near water, tall grass, forests, etc), and when I defeat one, the specific spawner will instantiate one of its opponents for that area.

 

Here is my code, so far:

var ThisSpot:String; var Oppos : GameObject[]; var OppoCount : GameObject[]; var HowMany = 3; var spawnSizeArea = 25; function Start() { StartCoroutine ("Spawn"); } function Update () { OppoCount = GameObject.FindGameObjectsWithTag("Respawn"); // OppoCount = GameObject.FindWithName(ThisSpot); This is what I am trying to achieve if(OppoCount.length < HowMany) { StartCoroutine ("Spawn"); } } function Spawn() { var length = Oppos.length-1; var index = Mathf.Round(length*UnityEngine.Random.value); curSpawn = Oppos[index]; var xz = Random.insideUnitCircle * spawnSizeArea; var newPosition = Vector3(xz.x,0,xz.x)+transform.position; yield WaitForSeconds(Random.Range(70,120)); Instantiate(curSpawn, newPosition, transform.rotation); // Then I could use these 2 lines of code below to use multiple spawners //var newOp : GameObject = Instantiate(curSpawn, newPosition, transform.rotation); //newOp.transform.Find("fighter").gameObject.name=ThisSpot; StopCoroutine("Spawn"); } 

Unity3d- Find all Game Objects with an Input string name (not Tag)

My goal is create multiple enemy spawners. My current spawner has a drag and drop variable for enemyprefabs. It looks for enemies tagged "ReSpawn", then checks if that length is below (x) to start spawning more. I would like to upgrade my current single spawner to still check for enemies namenamed respawn, but then check if those respawn have a child object named "blank" (an input string name I can specify in the editor).

EDIT - theEDIT: the solution. Instead of looking for gameobject names/tags, just keep track of a list

Unity3d- Find all Game Objects with an Input string name (not Tag)

My goal is create multiple enemy spawners. My current spawner has a drag and drop variable for enemyprefabs. It looks for enemies tagged "ReSpawn", then checks if that length is below (x) to start spawning more. I would like to upgrade my current single spawner to still check for enemies name respawn, but then check if those respawn have a child object named "blank" (an input string name I can specify in the editor).

EDIT - the solution. Instead of looking for gameobject names/tags, just keep track of a list

Find all Game Objects with an Input string name (not Tag)

My goal is create multiple enemy spawners. My current spawner has a drag and drop variable for enemyprefabs. It looks for enemies tagged "ReSpawn", then checks if that length is below (x) to start spawning more. I would like to upgrade my current single spawner to still check for enemies named respawn, but then check if those respawn have a child object named "blank" (an input string name I can specify in the editor).

EDIT: the solution. Instead of looking for gameobject names/tags, just keep track of a list

added 163 characters in body
Source Link
Epic447
  • 23
  • 1
  • 1
  • 4

EDIT - the solution. Instead of looking for gameobject names/tags, just keep track of a list

 **function Update () {        for (var i =0; i < OppoCount.length ; i ++)      if(OppoCount.length > 0){ if (OppoCount[i].activegameObject == falsenull) OppoCount.RemoveAt(i); }      if(OppoCount.length < HowMany){  StartCoroutine ("Spawn");  }  }      function Spawn(){    var length = Oppos.length-1;  var index = Mathf.Round(length*UnityEngine.Random.value);  curSpawn = Oppos[index];    //var newPosition = Random.insideUnitCircle* spawnSizeArea;  var xz = Random.insideUnitCircle * spawnSizeArea;  var newPosition = Vector3(xz.x,0,xz.x)+transform.position;      //yield WaitForSeconds(Random.Range(70,120));  yield WaitForSeconds(Random.Range(2,4));    //Instantiate(curSpawn, newPosition, transform.rotation);    var newOp : GameObject = Instantiate(curSpawn, newPosition, transform.rotation);    //newOp.transform.Find("fighter").gameObject.active = false;    OppoCount.Push(newOp);    StopCoroutine("Spawn");  }** 

EDIT

**function Update () { for (var i =0; i < OppoCount.length ; i ++) if(OppoCount.length > 0){ if (OppoCount[i].active == false) OppoCount.RemoveAt(i); } if(OppoCount.length < HowMany){ StartCoroutine ("Spawn"); } } function Spawn(){ var length = Oppos.length-1; var index = Mathf.Round(length*UnityEngine.Random.value); curSpawn = Oppos[index]; //var newPosition = Random.insideUnitCircle* spawnSizeArea; var xz = Random.insideUnitCircle * spawnSizeArea; var newPosition = Vector3(xz.x,0,xz.x)+transform.position; //yield WaitForSeconds(Random.Range(70,120)); yield WaitForSeconds(Random.Range(2,4)); //Instantiate(curSpawn, newPosition, transform.rotation); var newOp : GameObject = Instantiate(curSpawn, newPosition, transform.rotation); //newOp.transform.Find("fighter").gameObject.active = false; OppoCount.Push(newOp); StopCoroutine("Spawn"); }** 

EDIT - the solution. Instead of looking for gameobject names/tags, just keep track of a list

 **function Update () {        for (var i =0; i < OppoCount.length ; i ++)      if(OppoCount.length > 0){ if (OppoCount[i].gameObject == null) OppoCount.RemoveAt(i); }      if(OppoCount.length < HowMany){  StartCoroutine ("Spawn");  }  }      function Spawn(){    var length = Oppos.length-1;  var index = Mathf.Round(length*UnityEngine.Random.value);  curSpawn = Oppos[index];    //var newPosition = Random.insideUnitCircle* spawnSizeArea;  var xz = Random.insideUnitCircle * spawnSizeArea;  var newPosition = Vector3(xz.x,0,xz.x)+transform.position;      //yield WaitForSeconds(Random.Range(70,120));  yield WaitForSeconds(Random.Range(2,4));    //Instantiate(curSpawn, newPosition, transform.rotation);    var newOp : GameObject = Instantiate(curSpawn, newPosition, transform.rotation);    //newOp.transform.Find("fighter").gameObject.active = false;    OppoCount.Push(newOp);    StopCoroutine("Spawn");  }** 
added 1123 characters in body
Source Link
Epic447
  • 23
  • 1
  • 1
  • 4
Loading
deleted 114 characters in body; edited tags
Source Link
Tetrad
  • 30.1k
  • 12
  • 96
  • 143
Loading
Source Link
Epic447
  • 23
  • 1
  • 1
  • 4
Loading