Need help. Array based character customization

Hi ppl!

I’m new to Unity, but have a rather ambitious assignment. Avatar Customization. ~spooky music~ :frowning:

I decided i’m going with javascript, and using arrays. The logic works… well sorta. It works forward, for one step… no further… and i can’t switch back either.

here’s the script… Any help would be useful.

Thanks

var upperarr = new Array(4);
var upperI : int;
var a : int;
var i : int;	//for loop index
var myObj : GameObject;


function OnGUI () {

	if(GUI.Button( Rect(Screen.width-200,200,64,64), ">"))
	{
		upperSelector(true);
	}
	
	
	if(GUI.Button( Rect(Screen.width-300,200,64,64), "<"))
	{
		upperSelector(false);
	}

}


function upperSelector (x : boolean) {

	//for getting current index
	for(i=0; i<upperarr.length; i++)
	{
		if(upperarr[i].enabled == true)
		upperI = i;
	}
	
	if(!x)
	{
		a=upperI-1;
		
		if(a>=0  a<upperarr.length)
		{
			//for turning current index off
			myObj = upperarr[upperI];
			myObj.renderer.enabled = false;
		
			//for turning only selected index on
			myObj = upperarr[a];
			myObj.renderer.enabled = true;
		}
		else
		Debug.Log("Beginning of Array!");
	
	}
	else
	{
		a=upperI+1;
		
		if(a>=0  a<upperarr.length)
		{
			//for turning current index off
			myObj = upperarr[upperI];
			myObj.renderer.enabled = false;
		
			//for turning only selected index on
			myObj = upperarr[a];
			myObj.renderer.enabled = true;
		}
		else
		Debug.Log("End of Array!");
		
	}
	
}

Lets change your way of thinking.

First, having a wonderful true false, then adding one way or the other is great and all… but all that can be replaced with something much simpler. (change X to an int, and add it to the current item.)

Using an array is great and all, but… we need to add some functionality into it so that the user can edit things in the editor. So swap the array to a GameObject[ ]. This way we can set the number of the array in the editor and simply drag objects to it. (mind you, you need to drag active game objects, lest we have to change the code to instantiate things. If so, we need to change it. :wink: )

Next, we cycle through those objects and make them all inactive, then turn the first one back on.

OK, the whole thing would then work with using a new variable called currentUpper… This just keeps track of which object is supposed to be active.

Lastly, all that code to add things is simply changed to currentUpper + x… then enable it.

var upperarr : GameObject[];
var currentUpper : int;

function Start(){
	if(upperarr.Length>0){
		for(var obj : GameObject in upperarr){
			obj.active=false;
		}
		currentUpper=0;
		upperarr[currentUpper].active=true;
	}
}

function OnGUI () {
	if(upperarr.Length>0){
		if(GUI.Button( Rect(Screen.width-200,200,64,64), ">"))
		{
			upperSelector(1);
		}
		
		
		if(GUI.Button( Rect(Screen.width-300,200,64,64), "<"))
		{
			upperSelector(-1);
		}
	}

}


function upperSelector (x : int) {
	upperarr[currentUpper].active=false;
	currentUpper=Mathf.Repeat(currentUpper + x, upperarr.Length);
	upperarr[currentUpper].active=true;
}

@ BigMisterB

hi, tanx for the quick reply… but i found the error in my script. It was jus a syntax error. :sweat_smile:

upperarr___.renderer__.enabled_
The final func is like this…
```
*function upperSelector (x : boolean) {

[COLOR="yellowgreen"]//for getting current index[/COLOR]
for(i=0; i<upperarr.length; i++)
{
	if(upperarr[i].renderer.enabled == true)
	upperI = i;
}
Debug.Log(upperI);
if(!x)
{
	a=upperI-1;
	
	if(a>=0  a<upperarr.length)
	{
		[COLOR="yellowgreen"]//for turning current index off[/COLOR]
		myObj = upperarr[upperI];
		myObj.renderer.enabled = false;
	
		[COLOR="yellowgreen"]//for turning only selected index on[/COLOR]
		myObj = upperarr[a];
		myObj.renderer.enabled = true;
	}
	else
	Debug.Log("Beginning of Array!");

}
else
{
	a=upperI+1;
	
	if(a>=0  a<upperarr.length)
	{
		[COLOR="yellowgreen"]//for turning current index off[/COLOR]
		myObj = upperarr[upperI];
		myObj.renderer.enabled = false;
	
		[COLOR="yellowgreen"]//for turning only selected index on[/COLOR]
		myObj = upperarr[a];
		myObj.renderer.enabled = true;
	}
	else
	Debug.Log("End of Array!");
	
}

}*
```
It works. My principle is “Don’t Get Greedy!” :slight_smile:
thanks again