C# No0b here, need help!! PLEASE!!

Hello all,

Currently racking my brain over something that is probably childs play for many of you, please take a look at this and see if you can figure out where i’ve gone wrong…

I’m only posting the portion of code I’m currently having trouble with, if for some reason you require the rest of the code please let me know and i’ll gladly post it…Otherwise I’m going to need a new monitor and keyboard, and possibly another apartment once I get thrown in jail for dispatching it off the balcony…

CODE:

foreach(string data in countryList)
{
Application.LoadLevel (data);
}

ISSUE:

The code functions, HOWEVER it takes about 10 seconds for it to actually load the level, however If I simply remove the ‘foreach’ statement and replace with the following…

Application.LoadLevel(“United States”);

This works fine and loads the next level almost instantly…I realize the foreach method is within a loop, I’ve tried putting it within a boolean so that it’s only ran once, this also results in a 10 second load time…I NEED to be able to access the ‘data’ feature of the foreach loop…otherwise everything will not work. HOW can I use the ‘data’ function without foreach?? and/or how can I use foreach without running into this loop delay?

…again, i’m a C# noob, so if this question is vague or hard to understand please contact me and i’ll do my best to explain

Well, what are you trying to achieve? Usually you do a LoadLevel when it’s time to start a new level in the game. Telling Unity to load one level, then instantly load the next (throwing out the first), then load a third and throw out the second is, well, stupid. You might as well simply load the last level in the list, since that is what you’ll end up with after you’ve loaded each level and got rid of the one before. It’s taken 10 seconds to load every level in the list.

As Graham said: what you are actually doing with your code here is loading 10 different scenes in a loop. Application.LoadLevel will load each level and then upon loading the next will unload the current one. That is why your code takes about 10 seconds. You are really loading 10 levels in a row but only the last is displayed as only one level can be present at one time.

What you most likely want is to access individual elements. How to achieve this depends on what data type countryList is. Most common would be string[ ] or List judging from the code you have given us. You can now use the square bracket syntax to access any entry you want.

string[] countryList;

....

var level1 = countryList[0];
Application.LoadLevel(level1);

Keep in mind that an array is zero based (first index is 0 not 1)
I would suggest searching for the keyword “array” if you want to know more about this feature.

If you’re trying to combine the results of all the different scenes together, you should be using LoadLevelAdditive, not LoadLevel.

ah, I should have better worded my vague question…
so, what I have set up right now is a basic menu system, if the user clicks a GUI button it adds the associated country to the countryList variable. Once countryList.count > 0 an additional GUI button shows which allows the user to accept or cancel their selected choice (this is what countryList is controlling). If the user selects ‘accept’ then the mainCamera FOV will be reduced, once the mainCamera’s FOV falls below a set number the selected scene (or scene defined by the countryList var) is to load.

Currently this works, but again when using the foreach function it’s delayed. I think it’s because it’s caught in a loop / calculation? You can see the FOV continue to reduce although significanlty reduced in FPS (as expected) when code is locked up. It will eventually load however, this takes about 10 seconds. If I just replace the foreach function with a basic Application.loadLevel function it works fine. But then I don’t have access to the data portion that i did with the foreach and I no longer know how to call the needed data to load my scenes…

Here’s the full code, sorry it’s still a bit unorganized, work in progress lol…

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

public class gameManager : MonoBehaviour
{
	//VARIABLES
		//Bools
			bool mainMenuHome;
			bool Continue;
			bool exit;	
			bool looper;	
			bool newGame;
			bool waiting;
			bool options;
			bool pause;
	
		//Cameras
			public Camera mainCam; 
			public Camera countrySelection;	
	
		//Floats
			//Common
				float countrySelected;
				float countrySelectedRoll;	
				float waitTimeCur;
	
			//GUI
				//GUI Aspect Ratio
					float xAxis_GUI;
					float yAxis_GUI;
	
				//GUI Texture
					float alpha = 1;	//Alpha Color variable
					float fadeColorREF;

		//GameObjects
			public GameObject countryCam;
	
		//GUI Textures
			public GUITexture fadeColor;
	
		//Lists
			List<string> countryList = new List<string>();
			List<string> menuBtnList = new List<string>();
	
	//FUNCTIONS
		void Awake()
			{
				//Configure 'menuBtnList' default point
					if(Application.loadedLevelName == "mainMenu")
						{
							mainMenuHome = true;
							menuBtnList.Add ("newGame");
							menuBtnList.Add ("Continue");
							menuBtnList.Add ("options");
							menuBtnList.Add ("exit");			
						}
			}
	
		void Update()
			{
				//Load Level
					if(mainCam.fieldOfView <= 10)
						{
							foreach(string data in countryList)
								{
									Application.LoadLevel (data);
								}
						}			
		
				//Camera FOV correction
					countrySelection.fieldOfView = mainCam.fieldOfView;
		
				//On Input
					//Keyboard Input
						if(Input.GetKeyUp(KeyCode.UpArrow)  !waiting)
							{
								countrySelectedRoll += 1;
							}
				
						if(Input.GetKeyUp(KeyCode.DownArrow)  !waiting)
							{
								countrySelectedRoll -= 1;
							}
		
				//'countrySelected' function
					//mainCam FOV		
						if(countrySelected > 0)
							{
								mainCam.fieldOfView = Mathf.Lerp (mainCam.fieldOfView, 20, Time.smoothDeltaTime / 5);
							}
								else
									{
										mainCam.fieldOfView = Mathf.Lerp (mainCam.fieldOfView, 30, Time.smoothDeltaTime);
			
									}	
		
				//Main Camera Functionality
					if(looper == true)
						{
							mainCam.fieldOfView = Mathf.Lerp (mainCam.fieldOfView, 1, Time.smoothDeltaTime);
								if(mainCam.fieldOfView <= 1)
									looper = false;
						}
		
				//GUI Texture alpha control
					if(mainCam.fieldOfView > 30)
						{
							alpha = Mathf.Lerp (alpha, 1, Time.smoothDeltaTime); //Set Alpha to 'max'
							fadeColorREF = Mathf.Lerp (fadeColorREF, 0, Time.smoothDeltaTime); //Set fadeColorRef to 'Black'
							fadeColor.color = new Color (fadeColorREF,fadeColorREF,fadeColorREF, alpha);
						}
		
					if(mainCam.fieldOfView <= 30  mainCam.fieldOfView > 15)
						{
							alpha = Mathf.Lerp (alpha, 0, Time.smoothDeltaTime); //Set Alpha to 'max'
							fadeColorREF = Mathf.Lerp (fadeColorREF, 1, Time.smoothDeltaTime); //Set fadeColorRef to 'White'
							fadeColor.color = new Color (fadeColorREF,fadeColorREF,fadeColorREF, alpha);
						}
		
					if(mainCam.fieldOfView <= 15)
						{
							alpha = Mathf.Lerp (alpha, 1, Time.smoothDeltaTime); //Set Alpha to 'max'
							fadeColor.color = new Color (fadeColorREF,fadeColorREF,fadeColorREF, alpha);							
						}		
		
					if(countrySelected == 1)
						{			
							countryList.Add ("Africa");
							waiting = true;

						}

					if(countrySelected == 2)
						{
							countryList.Add ("Australia");
							waiting = true;

						}

					if(countrySelected == 3)
						{
							countryList.Add ("South America");
							waiting = true;

						}
		
					if(countrySelected == 4)
						{
							countryList.Add ("United States");
							waiting = true;

						}		
		
				//'waiting' function
					if(waiting)
						{
							waitTimeCur += Time.smoothDeltaTime;	//Increase 'waitTimeCur' float.

							}
		
				//Main Camera Rotation Control
					 if(countryList.Count > 0)
							{
								if(countryList.Contains("Africa"))
									countryCam.transform.localEulerAngles = new Vector3
										(
											Mathf.LerpAngle(countryCam.transform.localEulerAngles.x, 0, waitTimeCur),
											Mathf.LerpAngle(countryCam.transform.localEulerAngles.y, 0, waitTimeCur),
											Mathf.LerpAngle(countryCam.transform.localEulerAngles.z, 72, waitTimeCur)
										);
					
								if(countryList.Contains("Australia"))
									countryCam.transform.localEulerAngles = new Vector3
										(
											Mathf.LerpAngle(countryCam.transform.localEulerAngles.x, 340, waitTimeCur),
											Mathf.LerpAngle(countryCam.transform.localEulerAngles.y, 18, waitTimeCur),
											Mathf.LerpAngle(countryCam.transform.localEulerAngles.z, 315, waitTimeCur)
										);						
								
								if(countryList.Contains("South America"))
									countryCam.transform.localEulerAngles = new Vector3
										(
											Mathf.LerpAngle(countryCam.transform.localEulerAngles.x, 0, waitTimeCur),
											Mathf.LerpAngle(countryCam.transform.localEulerAngles.y, 0, waitTimeCur),
											Mathf.LerpAngle(countryCam.transform.localEulerAngles.z, 160, waitTimeCur)
										);
					
								if(countryList.Contains("United States"))
									countryCam.transform.localEulerAngles = new Vector3
										(
											Mathf.LerpAngle(countryCam.transform.localEulerAngles.x, 320, waitTimeCur),
											Mathf.LerpAngle(countryCam.transform.localEulerAngles.y, 352, waitTimeCur),
											Mathf.LerpAngle(countryCam.transform.localEulerAngles.z, 190, waitTimeCur)
										);
							}
								else
									{
										//InHouse Variables
											//Floats
												float zAxis = countryCam.transform.localEulerAngles.z;
			
												//Float Control			
													zAxis -= Time.smoothDeltaTime;
			
										//Release Camera Lock ((Retrun to basic rotation))
											countryCam.transform.localEulerAngles = new Vector3
												(
													countryCam.transform.localEulerAngles.x, countryCam.transform.localEulerAngles.y,zAxis
												);
									}
			}
	
		void OnGUI()
			{
				if(Application.loadedLevelName == "mainMenu")
					{
						//Main Menu Background
							GUI.Box (new Rect(Screen.width/2 - 125, Screen.height/2 - 125, 250, 250),""); //Main Menu Background
					
					}
		
				//Button Layouts
					if(mainMenuHome)
						{
							//Main Menu
								//New Game
									if(GUI.Button (new Rect (Screen.width / 2 - 50, Screen.height/2 - 25, 100, 25), "New Game"))
										{
											newGame = true;
											mainMenuHome = false;
										}
			
								//Continue
									if(GUI.Button (new Rect (Screen.width / 2 - 50, Screen.height/2 - 50, 100, 25), "Continue"))
										{
											Continue = true;
											mainMenuHome = false;
										}
			
								//Options
									if(GUI.Button (new Rect (Screen.width / 2 - 50, Screen.height/2, 100, 25), "Options"))
										{
											options = true;
											mainMenuHome = false;
										}			
			
								//Exit
									if(GUI.Button (new Rect (Screen.width / 2 - 50, Screen.height/2 + 25, 100, 25), "Exit"))
										{
											exit = true;
											mainMenuHome = false;
										}
						}
		
					 if(newGame)
						{
							//'countrySelected' function
								//Variable Countrol
									if(countrySelectedRoll > countryList.Count || countrySelectedRoll < 1)
										{
											countrySelectedRoll = 1;
										}
			
								//Next Country
									if(GUI.Button (new Rect(Screen.width/2 - 50, Screen.height/2, 100, 25), "Next"))
										{
											countrySelected = countrySelectedRoll;
											countrySelectedRoll += 1;				
										}
			
								//Previous Country
									if(GUI.Button (new Rect(Screen.width/2 - 50, Screen.height/2 + 25, 100, 25), "Previous"))
										{
											countrySelected = countrySelectedRoll;
											countrySelectedRoll -= 1;
										}			
			
							//Africa
								if(GUI.Button (new Rect(Screen.width/2, Screen.height/2 - 100, 100, 50), "Africa"))
									{
										countrySelected = 1;
									}
						
							//Australia
								if(GUI.Button (new Rect(Screen.width/2 - 100, Screen.height/2 - 50, 100, 50), "Australia"))
									{
										countrySelected = 2;				
									}
			
							//South America
								if(GUI.Button (new Rect(Screen.width/2, Screen.height/2 + 50, 50, 50), "South America"))
									{
										countrySelected = 3;				
									}			
			
							//United States
								if(GUI.Button (new Rect(Screen.width/2 - 100, Screen.height/2, 50, 50), "United States"))
									{
										countrySelected = 4;				
									}			
	
							//Are You Sure?		
								if(countryList.Count > 0)
									{
										if(GUI.Button (new Rect(Screen.width/2, Screen.height/2 - 50, 50, 50), "Yes"))
											{
												looper = true;
											}
					
										if(GUI.Button (new Rect(Screen.width/2 + 50, Screen.height/2 - 50, 50, 50), "No"))
											{
												looper = false;					
												countryList.Clear();
												waiting = false;
												waitTimeCur = 0;
												countrySelected = 0;
												mainCam.fieldOfView = Mathf.Lerp (mainCam.fieldOfView, 30, Time.smoothDeltaTime);
											}			
									}
						}
					
					if(Continue)
						{
							//Application.LoadLevel("");
						}					
				
					if(options)
						{
							//Show Options Menu
						}		

					if(exit)
						{
							//Display 'ARE YOU SURE' 
								//Then
								Application.Quit();
						}
		
				//Pause Menu 		
					if(!pause)
						{
							GUI.Button (new Rect(Screen.width - 75, Screen.height - Screen.height + 25, 50, 50), "Pause");
				
						}
			}
}

Your design doesn’t make sense. Initially you talk about countryList as if it’s a collection of things:

but later you talk about it as if it’s a single thing:

Both can’t be true. My guess is: you don’t actually want countryList to be a list, because you’re only supposed to be able to select one country at a time. Is that correct?

…it’s actually designed in such a way that the list will only ever contain a single item. So in-fact, both are true.
I’ve done this intentionally however, being that i’m referencing the ‘data’ within the list.

Basically…

You click ‘North America’

  • countryList then = northAmerica

ARE YOU SURE is displayed

  • you click yes
  • Load scene titled “northAmerica” ((this is the ‘data’ portion of the foreach statement referenced previously))
  • if you click NO countryList = cleared ((there will only ever be one item within the list at any given time))

If this isn’t helpful I’m not to sure how else to describe what I’m attempting to do…

If there’s only ever one item in the list, there’s no need to use a list. Just use a single variable of whatever type the list contents are (in this case, string).

I’ll post the final code once finished so that maybe you guys can help me refine it or correct it…but I’ve just figured out what was causing the delay just now ((code redundancy)). It works the way I’ve developed it and I’m thinking less code in the long run, but it’s possible that I’ve poorly explained the scenario…

If you care to look the delay seems to be caused from the countrySelected float, ive noticed that lines #28 - 54 are really not needed at all, restructured a few things and it seems to be working just fine now.

In your game Africa and South America are countries?

Next up, if your countrySelected variable is only ever discrete integer values, why are you using a float? And that being said, the code that you removed was comparing floats to ints then, which required the ints to then be converted to floats (dunno if the compiler would have optimized this or not, there’s a good chance not).

Yeah you’re probably right XGundam, after I took those lines out it sped up without any problem. I’m sure there’s a better way to write this but since it’s working I’ve continued on with it, mostly learning as I go. I think after a few things I picked up earlier I should be able to finish this up and get everything running smoothly.

I didn’t bother creating a separate list to contain continents, as I already have the list for countries created and it’s capable of carrying information for both. Just didn’t feel anyone was going to point out the obvious but, ya know…this is the internet.