SetActiveRecursively- Trouble shoot

I can’t seem to get this to work. what am I doing wrong? The goal is to make it so that my character switches with model is active but thats not working, it seems that none of the models are being set to the list, and also that the default model is not being set as true.

`
for(var j:int=0; j < charList.length; j++)
{
charObjectList[j]=charList[j].object;
}

for(var i:int=0; i<charObjectList.length; i++)  
{  
	charObjectList*.SetActiveRecursively(false);* 
  • }*

  • currentCharID=0;*

  • currentChar=charObjectList[0];*

  • currentChar.SetActiveRecursively(true);*

  • charController=gameObject.GetComponent(CharacterController);*
    `
    If anymore info is needed ask away, I omitted the code where I set up the arrays but its pretty straight forward.

2 Answers

2

I would do the character selection this way:

function SelectCharacter(num: int){ // select character[num]
    for(var j:int=0; j < charList.length; j++){
        var character: GameObject = charList[j].object;
        if (j == num){ // if the object is the one you're looking for...
            character.SetActiveRecursively(true); // activate it...
            currentChar = character; // and save it in currentChar
        } else { // but if is another character...
            character.SetActiveRecursively(false); // deactivate it.
        }
    }
    currentCharID = num; 
    // get the CharacterController of the selected character
    charController = currentChar.GetComponent(CharacterController);
}

The way you was doing that before seemed wrong because in the last line you were getting the CharacterController from the object which had that script, but I suspect you actually wanted the selected character’s controller.

Don’t know if this will help, but the way I currently switch GameObjects is by having them as children of an empty GameObject and using “GetChild”.

eg:
public int currentRace = 0;

void switchRace(int index){
	for(int i = 0; i < transform.childCount; i++ ){
		if(i == index){
			transform.GetChild(i).gameObject.SetActiveRecursively(true);	
		}else{
			transform.GetChild(i).gameObject.SetActiveRecursively(false);
		}
	}
}

// Use this for initialization
void Start () {
	switchRace(currentRace);	
}

// Update is called once per frame
void Update () {
	
	if(Input.GetKeyDown("left")){
		if(currentRace > 0){
			currentRace--;
			switchRace(currentRace);	
		}
	}else if(Input.GetKeyDown("right")){
		if(currentRace < transform.childCount-1){
			currentRace++;
			switchRace(currentRace);	
		}
	}

}

This works for me for now but I am definitely working to make it better.