- Home /
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[i].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;
}
I m not sure but try putting your loop like this
for (var i : int = 1 ; i
Answer by haim96 · Apr 10, 2014 at 06:53 AM
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.
var Canada : GameObject[];
Should mean there is enough i think.
Answer by maddFrogg · Apr 10, 2014 at 06:59 AM
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[i].GetComponent<colorChange>().colourEnd = new Color(1,1,1);
}
}
}
Note I also changed the GetComponent statement, I usually use it this way...
This didn't work, when i tried to use the GetComponent like you do it would just give me errors.
I'm stuck at this
Canada[i].GetComponent(colorChange).colourEnd = new Color(1,1,1);
What is colorChange is it a method?
You may try this
Canada[i].GetComponent<_NameOfYourClass_>().colourEnd = new Color(1,1,1);
Where NameOfYourClass is the name of the class you posted, containing the colourEnd variable and stuff
Your answer
Follow this Question
Related Questions
Using arrays and for loop with GetComponent error 1 Answer
How do i get script variables from a prefab inside a list? 1 Answer
Loop through Children Unable to Access Component Script 1 Answer
Guarantee loop order of child objects. 1 Answer
I dont know why im getting a null reference exception 1 Answer