Loop For Problem!

Hi! This is my script but is do not work properly

var textur:Texture[];
var mesi:GameObject[];
static var idx:int;   


function Update(){
                  renderer.material.mainTexture=textur[ idx ] ; 
                  attivaMeseCorrente(idx);
                  }


function attivaMeseCorrente(index : int){
                                         for (var i=0;i<12;i++){
                                                               if (i== index){
                                                                              mesi[idx].SetActiveRecursively(true);
                                                                             }
                                                               else{
                                                                    mesi[idx] .SetActiveRecursively(false);
                                                                   }
                                                                }               
                                         }

I would set active only the object indicated by the VAR idx (it is changed in another script).

With this script will be actived only the object [12] when idx = 12
But if for example idx = 7, object [7] doesn’t active and I don’t know why!

ps: object [12] is actived properly but if idx become 11 or other, object [12]do not turn off :face_with_spiral_eyes:

print and check the idx value?? my guess is tht it is not changing from 12?!!! also print and see wat the index value is

coz i do the exact same thing for my game… and it works!

ok I have inserted a Debug.Log(idx) and it return the correct number… 1,2,3,4,5,6,7,8,9,10,11

Only when idx = 11 the object [11] is actived!

I noticed that if I change for (var i=0;i<7;i++)

Only when idx = 7 the object [7] is actived!

hmm i guess the only possibility is tht idx changes through 0-11 all in one frame!!
so it remains as 11 as per the update of the above script …Did u try Debug.Log(index);

can u paste the code tht edits the value of idx!!

this is the script

var suono:AudioSource;

function OnMouseDown(){
suono.Play();
var script : mese = GameObject.Find("Cubo Mese").GetComponent(mese);
if(mese.idx<=10){
mese.idx++;
}
else
{
mese.idx=0;
}
}

pheraps problem is that I’m calling the attivaMeseCorrente function on Update?

hmm im not sure everything is fine here…

try to change it like the following it ma work… just a guess…

var textur:Texture[];
var mesi:GameObject[];
static var idx:int;   


function Update(){
                       
               }


function attivaMeseCorrente(index : int){
                                         for (var i=0;i<12;i++){
                                                               if (i== index){
                                                                              mesi[idx].SetActiveRecursively(true);
                                                                             }
                                                               else{
                                                                    mesi[idx] .SetActiveRecursively(false);
                                                                   }
                                                                }               
                                         }
function OnMouseDown(){
suono.Play();
var script : mese = GameObject.Find("Cubo Mese").GetComponent(mese);
if(mese.idx<=10){
mese.idx++;

         script.gameObject. renderer.material.mainTexture=script.textur[ idx ] ; 
         script.attivaMeseCorrente(idx);
}
else
{
mese.idx=0;
}
}

jus a guess try it !!

uhm no… I tryed and do not work and however, this disrupts my script…

I think that I need an help with the for Loop ! uhm… We wait for other helpers :slight_smile:

hmmm did u try printing ’ index’ inside attivaMeseCorrente?!?!?! tht is wat decides the loop rite!

I tryed now and the value is always correct 1,2,3,4,5,6,7,8,9,10,11

the question is: Why if I set

for (var i=0;i<6;i++){

the only object that will be activated will be the [6]???

ps 6 is an example. I have the same behavior with any number

You want to access mesi instead of mesi[idx]. You are not actually looping through all of mesi, but rather setting the same one over and over 12 times.

ah lol kelly is rite … it is a simple problem :stuck_out_tongue:

oh yes!! Thanks Kelly G and flamy!!