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