Problem with arrays

i have this script to switch between different characters in an array:

 public Transform[] target;
 if(Input.GetAxis ("Mouse ScrollWheel") > .1){
    			target*.GetComponent<walk>().enabled = false;*
  •  	if(i == 1){*
    
  •  		i = 0;*
    
  •  		return;*
    
  •  	}*
    
  •  	i++;*
    
  •  }*
    
  •  if(Input.GetAxis("Mouse ScrollWheel") < -.1){*
    

_ target*.GetComponent().enabled = false;_
_
if(i == 0){_
_
i = 1;_
_
return;_
_
}_
_
i–;_
_
}*_
how can i rewrite lines 4-7 and 12-15 if i do not know how many items are in the array, unlike in this script where there is 2 items in the array?

If instead of an array, you use a List, you can use the Count parameter to tell how many are in the array.

Will post example in a moment.

List<string> myString = new List<string>();
int listSize;
 
void Start () {
listSize = myString.Count;
}

use an index you increase when scrolling:

int index = 0;
void Update(){
   float scroll = Input.GetAxis("Mouse ScrollWheel");
   if(scroll > 0){
      if(++index > array.Length)index = 0;
   } 
   else if (scroll < 0){
     if(--index < 0)index = array.Length - 1;
   }
}

Now you can use index to pick your character.