Using array to scroll through game objects, having issues..

I’m in a programming class and we need to do some AI work this week. The basis of the assignment is to have multiple rooms with “Guards” and control them using specific key commands. We have an object that will navigate the rooms, but if detected my a guard, will restart. I’m still new-ish to programming, so some of these things don’t make sense to me. Here’s the starting of my scripting. When the arrow keys are hit we are supposed to scroll through each guard in the scene and be able to control their movements to have them avoid the player.

using UnityEngine;
using System.Collections;

public class DebugManager : MonoBehaviour {


	public GuardData[] guards;
	
	GuardStateMachine gsm;
	
	private GameObject theGuards;


	// Use this for initialization
	void Start () {
	
		 guards = GameObject.FindObjectsOfType<GuardData>();
		 
		 gsm = GetComponent<GuardStateMachine>();
		 
		 

		
	}
	
	// Update is called once per frame
	void Update () {
	
		if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow))
		{
			for (int i = 1; i < guards.Length; i++)
			{
				Debug.Log("Input");
				theGuards = GameObject.FindGameObjectWithTag("Guard");
				theGuards.renderer.material.color = Color.blue;
				
			}
			
		
		
			if (Input.GetKey(KeyCode.Alpha1))
			{
				
			}
			
			if (Input.GetKey(KeyCode.Alpha2))
			{
				
			}
			
			if (Input.GetKey(KeyCode.Alpha3))
			{
				
			}
			
			if (Input.GetKey (KeyCode.Alpha4))
			{
				
			}
		
		
		}
		

	
	}
	
}

Thank you guys for any help offered.

Hi, with this being a School assignment I won’t really give you an answer but more a direction to go in :wink: You already get all the guards in the Start method using the line “guards = GameObject.FindObjectsOfType();”. And, I’m sure you’ve covered this in class, an element in an array is accessed using an index. You may want to keep a persistent index that you increment and decrement when a key is pressed:

// pseudo code
object[] listOfObjects;
int currentIndex = 0;

// Example scroll
public void Next()
{
    currentIndex++;
    if(currentIndex > listOfObjects.Length)
    {
        currentIndex = 0;
    }
}

public void UpdateObject()
{
    object currentObj = listOfObject[currentIndex];
    // Do what you want with the ojbect
}

In the examples I used objects but this can be replaced by your EnemyData. And, in the UpdateObject method you could pass the input into the enemy to make them move. You can see how I have broken the implementation down into multiple methods, this is good practice and simplifies code and makes it easier to maintain. I hope this helps =)