list = list, index problem

Hi,

I’m working on a menu, two GUI windows.
The first menu is generated and named by the enumerator types in my ‘Items.cs’ Script.

The second is generating buttons based on the type, so if ‘Notes’ in menu one is clicked, window two will display all items with type ‘Note’.

My problem is that the window two focus control, only works for my drawNotes screen, and the handle window two function only uses the drawNotes elements.

I thought about adding

newArray[i] = bag[i];

inside the for loop, to set all the buttons of the correct type into an array, then use that somehow within my switch statement, to get it to act dependent on the array…

Unfortunately the line above gives me this error :

 ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[items].set_Item (Int32 index, .items value) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:639)
menu.drawNotes () (at Assets/menu.cs:207)
menu.windowTwoFunc (Int32 id) (at Assets/menu.cs:145)
UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style)

It’s a big script (in terms of reading it on this site, so I’ll post it below, and i’ll also link to the project, if anyone wouldn’t mind opening it themselves and taking a look if it’s easier )

Here’s the link to the project: It’s just two scripts and a main camera, so the files really small

EDIT: The problem resides in my ‘drawNotes()’ menu always taking charge of the gui.focus , even when I use drawKeys or drawWeapons, the notes holds the focus

Thanks for anyone that helps, I realise theres a lot to sift through possibly

Menu.CS

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class menu : MonoBehaviour {

	Rect windowOneRect = new Rect (10, 10, 150, 150);
	Rect windowTwoRect = new Rect (200, 10, 350, 150);

	List<items> bag = new List<items>();
	List<items> noteList = new List<items>();

	string[] itemArray;

	bool drawWeaponsBool = false;
	bool drawNotesBool = false;
	bool drawKeysBool = false;

	bool showWindowOne = true;
	bool showWindowTwo = false;

	int windowOneIndex = 0;
	int windowTwoIndex = 0;
	
	int tempInt = 0;
	
	void Start () {
	
		itemArray = System.Enum.GetNames(typeof(items.objectType));

		bag.Add (new items ("Note01", items.objectType.note));
		bag.Add (new items ("Note02", items.objectType.note));
		bag.Add (new items ("Note03", items.objectType.note));
		bag.Add (new items ("Note04", items.objectType.note));

		bag.Add (new items ("Key01", items.objectType.key));
		bag.Add (new items ("Key02", items.objectType.key));

		bag.Add (new items ("Weapon01", items.objectType.weapon));
		bag.Add (new items ("Weapon02", items.objectType.weapon));

	}

	void OnGUI () {


		// Just to check what gets added to the array, save messing in the inspector 
		if(noteList.Count > 0) {

	    		int y = 100;

			for(int i = 0; i < noteList.Count; i++){

			     	GUI.Label (new Rect (Screen.width - 270, y, 150, 25), "New Array " + i + noteList[i].name);

		    		y+=30;
		    	}

	     	}

		//Just checking values in the GUI 
			if (bag.Count > 0) {
					 
						GUI.Label (new Rect (Screen.width - 270, 20, 150, 25), "Window One Index = " + windowOneIndex.ToString ());
			            GUI.Label (new Rect (Screen.width - 270, 50, 240, 25), "Number of items in this catagory: " + (windowTwoIndex + 1 ).ToString());
			            GUI.Label (new Rect (Screen.width - 270, 80, 240, 25), "WindowTwo index bag.Name = " + bag[windowTwoIndex].name);

						if (showWindowOne == true) {

				                GUI.FocusControl(itemArray[windowOneIndex]);
								GUI.Window (0, windowOneRect, windowOneFunc, "Window One");

						} 

						if (showWindowTwo == true) {

								GUI.Window (1, windowTwoRect, windowTwoFunc, "Window Two");
	
						}
	     	
				}

	}

	void windowOneFunc(int id){

		int y = 30;

		for (int i = 0; i < System.Enum.GetValues(typeof(items.objectType)).Length; i ++) {

			GUI.SetNextControlName(itemArray [i]);
			GUI.Button (new Rect (10, y, 100, 25), itemArray [i]);
			
			y += 30;
		}

		if (Input.GetKeyDown (KeyCode.E)) {
			windowOneHandler ();
		}

	}

	void windowOneHandler(){
		
	switch (windowOneIndex) {

		case 0: 
			print("Clicked " + itemArray[windowOneIndex]);
			drawNotesBool = true;
			drawWeaponsBool = false;
			drawKeysBool = false;
			showWindowOne = false;
			showWindowTwo = true;
			return;

		case 1: 
			print("Clicked " + itemArray[windowOneIndex]);
			drawKeysBool = true;
			drawWeaponsBool = false;
			drawNotesBool = false;
			showWindowOne = false;
			showWindowTwo = true;
			return;

		case 2: 
			print("Clicked " + itemArray[windowOneIndex]);
			drawWeaponsBool = true;
			drawNotesBool = false;
			drawKeysBool = false;
			showWindowOne = false;
			showWindowTwo = true;
			return; 

		default:
			print("No option here");
			return;
	
	}
		
	}
	
	void windowTwoFunc(int id){
		
	    if (drawNotesBool == true) {
			drawNotes ();
		}
		
		if (drawWeaponsBool == true) {
			drawKeys ();
		}
		
		if (drawKeysBool == true) {
			drawWeapons ();
		}
		
		if (Input.GetKeyDown (KeyCode.Backspace)) {
			showWindowTwo = false;
			showWindowOne = true;
		}
		
		if (Input.GetKeyDown (KeyCode.E)) {
			windowTwoHandler ();
		}

	}
	
	void windowTwoHandler(){
		
		switch (windowTwoIndex) {
			
		case 0: 
			print("Clicked " + bag[windowTwoIndex].name);
			return;
			
		case 1: 
			print("Clicked " + bag[windowTwoIndex].name);
			return;
			
		case 2: 
			print("Clicked " + bag[windowTwoIndex].name);
			return; 

		case 3: 
			print("Clicked " + bag[windowTwoIndex].name);
			return; 

		default:
			print("No option here");
			return;
			
		}
		
	}

	void drawNotes(){

		GUI.FocusControl(bag[windowTwoIndex].name);

		int y = 20;
		
		int ammount = 0;
		
		for (int i = 0; i < bag.Count; i ++) {

			if(bag[i].enumVar == items.objectType.note){

			    noteList[i] = bag[i];

				GUI.SetNextControlName(bag[i].name);
				GUI.Button (new Rect (10, y, 100, 25), bag [i].name);

				ammount ++;
				
				y += 30;

			}
			
		}
		
		tempInt = ammount;
	
	}

	void drawKeys(){

		GUI.FocusControl(bag[windowTwoIndex].name);

		int ammount = 0;

		int y = 20;
		
		for (int i = 0; i < bag.Count; i ++) {
			
			if(bag[i].enumVar == items.objectType.key){

				GUI.SetNextControlName(bag[i].name);
				GUI.Button (new Rect (10, y, 100, 25), bag [i].name);

				ammount ++;

				y += 30;

			}

		}
	
		tempInt = ammount;
		
	}

	void drawWeapons(){

		GUI.FocusControl(bag[windowTwoIndex].name);

		int ammount = 0;

		int y = 20;
		
		for (int i = 0; i < bag.Count; i ++) {
			
			if(bag[i].enumVar == items.objectType.weapon){

				GUI.SetNextControlName(bag[i].name);
				GUI.Button (new Rect (10, y, 100, 25), bag [i].name);

				ammount ++;

				y += 30;

			}	

		}

		tempInt = ammount;

	}

	

	void Update(){
		
		//Contains the keyboard input to move through the index's of each window
		if (showWindowOne == true) {
			
			if (Input.GetKeyDown (KeyCode.UpArrow)) {
				if (windowOneIndex == 0) {
					windowOneIndex = itemArray.Length - 1;
				} else { 
					windowOneIndex --;
				}
			}
			
			if (Input.GetKeyDown (KeyCode.DownArrow)) {
				if (windowOneIndex == itemArray.Length - 1) {
					windowOneIndex = 0;
				} else { 
					windowOneIndex ++;
				}
			}
			
		}
		
		if (showWindowTwo == true) {
			
			if (Input.GetKeyDown (KeyCode.UpArrow)) {
				if (windowTwoIndex == 0) {
					windowTwoIndex = tempInt - 1;
				} else { 
					windowTwoIndex --;
				}
			}
			
			if (Input.GetKeyDown (KeyCode.DownArrow)) {
				if (windowTwoIndex == tempInt - 1) {
					windowTwoIndex = 0;
				} else { 
					windowTwoIndex ++;
				}
			}
			
			
		}
	}




}

Items.cs

using UnityEngine;
using System.Collections;

[System.Serializable]
public class items {

	// Use this for initialization

	public string name;


	public enum objectType { 

		note, 
		weapon,
		key

	}
	
	public objectType enumVar;

	// Update is called once per frame
	public items(string newName,  objectType newType){

		name = newName;
		enumVar = newType;

	}
}

I must say that it is a bit confusing… however, doing this…

	void windowOneFunc(int id){
		
		int y = 30;
		
		for (int i = 0; i < System.Enum.GetValues(typeof(Item.objectType)).Length; i ++) {
			
			GUI.SetNextControlName(itemArray [i]);
			if(GUI.Button (new Rect (10, y, 100, 25), itemArray [i])){
				windowOneIndex = i;
				windowOneHandler();
			}
			
			y += 30;
		}
		
		if (Input.GetKeyDown (KeyCode.E)) {
			windowOneIndex = 1;
			windowOneHandler ();
		}
		
	}

Netted some results.

The weapons and keys worked fine.

So I looked at the function you are using to draw the notes and compared it to the others.

The only difference is this line:

noteList[i] = bag[i];

so I commented it, and it worked just fine.

This lead me to think… How many bags, and how many noteList items there are. to which I recieved 0 and 8. So there are 8 bags and no note lists.

Now the confusing part is what all this stuff is, but I am sure it makes sense to you. :wink:

Debug.Log (noteList.Count);
Debug.Log (bag.Count);

Thank you for taking the time to help, I realise the code is a bit messy.

Your solution looks fine, but unfortunately it’s for the wrong section, it’s the content in the windowTwo function that is the problem, that is my bad it wasn’t clear in my original post (i’ll edit it now)

The bag is my main inventory, anything I pick up is an ‘item’ that adds to the bag.

I then try and sift through the bag depending on what button is pressed, note… key… weapon, then display them all.

The current problem is being able to, upon drawNote() or drawKey() is be able to seperate them in the window.

Its hard to explain, but if I debug.log ( GUI.GetNameOfFocusedControl()); )

I get only items from my ‘drawNote()’ functions. Even though the buttons in each window show the appropriate names

Example, you can see form the attached image that ‘Note01’ at the bottom of my console window is being printed regardless what window I’m in. Which consequently is taking hold of the GUI Focus