- Home /
findgameobjectswithtag focusing on 1 object
So I'm making a 2d game where you go around mining minerals and in order to see what's ahead you need to be next to a block to turn off a SpriteRenderer component so you can see what the block is. But when I wanted to make torches to keep the blocks uncovered even while you're not standing next to it, the blocks only uncover for 1 torch. I want it to work for multiple torches. Heres the code that goes on the mineral. I think I did the for loop wrong. Can't seem to find solution anywhere.
var cover : GameObject; //the cover
var mineHelm1 = false; //mining helmet upgrade to see farther
var mineHelm2 = false; //upgrade 2
var torchInRange = false; //turns true so that blocks ignore the player and use torch to uncover instead
function Start () {
}
function Update () {
var torches : GameObject[] = GameObject.FindGameObjectsWithTag("torch");
var range = Vector3.Distance(this.transform.position,GameObject.Find("Player").transform.position);
//^^ distance between this block and player
if(range <= 2.7f && mineHelm1 == false && mineHelm2 == false && torchInRange == false){//to detect if player is close
cover.GetComponent(SpriteRenderer).enabled = false;
}
else if(range <= 5.3f && mineHelm1 == true && torchInRange == false){
cover.GetComponent(SpriteRenderer).enabled = false;
}
else if(range <= 7.9f && mineHelm2 == true && torchInRange == false){
cover.GetComponent(SpriteRenderer).enabled = false;
}
else if(range >= 2.7f && mineHelm1 == false && mineHelm2 == false && torchInRange == false){ //to turn cover back on
cover.GetComponent(SpriteRenderer).enabled = true;
}
else if(range >= 5.3f && mineHelm1 == true && torchInRange == false){
cover.GetComponent(SpriteRenderer).enabled = true;
}
else if(range >= 7.9f && mineHelm2 == true && torchInRange == false){
cover.GetComponent(SpriteRenderer).enabled = true;
}
for(var torch : GameObject in torches){ //this is part where it looks for torches and turns off cover sprites
var rangeTorches = Vector3.Distance(this.transform.position,torch.transform.position);
//^^ distance between block and torch
if (rangeTorches <= 7.9f){ //to turn cover off
cover.GetComponent(SpriteRenderer).enabled = false;
torchInRange = true;
}
else if (rangeTorches >= 7.9f){ //to turn back on
cover.GetComponent(SpriteRenderer).enabled = true;
torchInRange = false;
}
}
}
I can't see an obvious mistake in your code.
$$anonymous$$aybe the torches are not correctly tagged with "torch"? But another hint once you got this working: Using any GameObject.Find* function in an Update() of many game objects (like in "every coverblock") is going to kill your performance VERY quickly. You should use static Lists where the torches/player are add/remove themself on OnEnable/OnDisable.
Edit: Scratch this. GameVortex had the superiour answer. ;)
Answer by GameVortex · Nov 14, 2014 at 07:49 AM
After you have found a torch in range you still continue the loop to check for more torches. This means that it is the last torch in the loop that will be used, and it is highly unlikely that torch is the one that is closest. You need to exit the loop as soon as you find a torch in range. You can do that with the break command:
if (rangeTorches <= 7.9f){ //to turn cover off
cover.GetComponent(SpriteRenderer).enabled = false;
torchInRange = true;
break; //break here to exit the loop because we found a torch nearby
}
By the way, you are using both FindGameObjectWithTag and Find functions at the start of the Update. These are slow functions not recommended to be used in each Update. You also probably have a lot of these mining objects in your game, and they Find functions will most likely cause lag problems eventually. I recommend doing the check in a Torch script instead when it is placed and then storing the result. Likewise you can do the player range check on the Player instead, and apply the range to the nearby mining objects.
Your answer
![](https://koobas.hobune.stream/wayback/20220613171452im_/https://answers.unity.com/themes/thub/images/avi.jpg)