FindGameObjectsWithTag Changing Variable on Another Script Not Fully Working

As the title says i’m trying to change a colour variable on multiple objects through a gui button press. At the moment when i click the button it changes most of the objects but for seemingly no reason doesn’t effect some. They all have the exact same tags and components yet they remain unaltered.

    f

unction OnGUI(){
        	Canada = GameObject.FindGameObjectsWithTag("Canada");
        	
        	if(GUI.Button(Rect(10,10,100,30),"Button Label")){
        		if(showMenu == false)
        			showMenu = true;
        		else
        			showMenu = false;
        	}
        
            if(showMenu == true){
                if (GUI.Button(Rect(10, 50, 100, 30 ), "One")){
        
        			for (var i : int = 0; i < Canada.length; i++){
        				Canada*.GetComponent(colorChange).colourEnd = new Color(1,1,1);*

}

My first thought was a there was a problem with the loop so i changed ‘Canada.length’ to a high number thinking it might change everything, but i had no luck with this.

Any help is much appreciated, thank you in advance.

EDIT I figured it might help to post the code be used on the objects i’m trying to effect.

var colourStart: Color = Color.red;
var colourEnd: Color = Color.red;;

function Start() {

}

function Update () {

_ renderer.material.color = Color.Lerp (colourStart, colourEnd, Time.deltaTime * 3);_

  • colourStart = rrenderer.material.color;*

}

does the number of object with tag “canada” changed during running?
if not i would suggest to move Canada = GameObject.FindGameObjectsWithTag("Canada");
to Start() so it will happen only once when the script start and not every time that gui update.

also if canada is an array(and it should be), make sure you declared enough for all the object in scene.

Hi, I’d remove this line from where it is written

Canada = GameObject.FindGameObjectsWithTag("Canada");

as it executes in every frame with no use at all, and I’d place it here:

[...]
if(showMenu == true){
    if (GUI.Button(Rect(10, 50, 100, 30 ), "One")){
        Canada = GameObject.FindGameObjectsWithTag("Canada");
        for (var i : int = 0; i < Canada.length; i++){
             Canada*.GetComponent<colorChange>().colourEnd = new Color(1,1,1);*

}
}
}
Note I also changed the GetComponent statement, I usually use it this way…