Change variable in all gameObjects with a certain tag and distance

Hi everyone,

I need to do two things: first get all the objects with a certain tag, at a certain distance, then change one variable in this gameObject

Here is my code:

private var randomCreate = 0.0;

var upkeepWaterObjects = GameObject.FindGameObjectsWithTag ("upkeepWater"); 

function OnTriggerEnter (other : Collider) 
{


	if (Vector3.Distance(GameObject.FindWithTag("upkeepWater").GetComponent(Transform).position, transform.position)<100)
	}
	for (var ressourceUpkeep in upkeepWaterObjects)
	{

	var other2 = upkeepWaterObjects.GetComponent(ressourceUpkeep);
	other2.waterUpkeep +=5;
	}
	}
	
	Destroy (gameObject, 0);

Thanks a LOT in advance
Cheers
HAppy St Patrick :wink:

This script doesn’t make much sense! Why OnTriggerEnter? Why Destroy(gameObject)?

If you want to select all objects tagged “upkeepWater” inside a 100m range, get an array with all “upkeepWater” objects at Start (Awake or code outside any function may be executed when not all objects were created yet!). When needed, call the function SetVariables() below to select objects inside the range and increment their waterUpkeep variables:

private var randomCreate = 0.0;
var upkeepWaterObjects: GameObject[];

function Start(){ // get all objects tagged "upkeepWater" in the array:
  upkeepWaterObjects = GameObject.FindGameObjectsWithTag ("upkeepWater"); 
}

// call this function to add 5 to the variable waterUpkeep in each object
// tagged "upkeepWater" that's at 100m or less of this object:

function SetVariables(){
  for (var upkeep in upkeepWaterObjects){
    if (Vector3.Distance(upkeep.transform.position, transform.position)< 100){
      // get the script (replace ScriptName with the actual script name!):
      var other2 = upkeep.GetComponent(ScriptName);
      other2.waterUpkeep +=5;
    }
  }
}