How to restart Maze script for the next level?

Hi guys,

Don’t get me wrong I’m new to C# and the script I’m using was made by someone else but I have added a fair bit. :smile:

Anyway my question is I would like the script go to the next level after the player has reached the exit. To do this I would imagine the main script would be reloaded with new parameters but my problem is how to do this using the current script? I have an exit script which works but only restarts level one.

The main script Grid.cs:

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

public class Grid : MonoBehaviour {
	
	//* -> Assigned in Inspector
	public Transform[] CellPrefab;//* Prefab array range from 1 - 4
	public Transform ExitPrefab;
	public Vector3 GridSize;//*
	public float Buffer;//*
		
	public List<Transform> Set = new List<Transform>();
	public List<Transform> CompletedSet = new List<Transform>();
	
	public List<Vector3> Directions = new List<Vector3>();//*
			
	void Start ()
	{
		CreateGrid ();
		SetStart (0, 0);
	}
	
	Transform[,] GridArr;
	
	public void CreateGrid ()
	{
		int x = (int)GridSize.x;
		int z = (int)GridSize.z;
		int cn = 0;
		GridArr = new Transform[x, z];
		Transform newCell;
		for (int ix = 0; ix < x; ix++) {
			for (int iz = 0; iz < z; iz++) {
				newCell = (Transform)Instantiate (CellPrefab[Random.Range(0,4)], new Vector3 (ix, 0, iz) * Buffer, Quaternion.identity);
				newCell.name = "Cell" + cn++;
				newCell.parent = transform;
				newCell.GetComponent<Cell>().Position = new Vector3 (ix, 0, iz);
				GridArr [ix, iz] = newCell;
			}
		}
	}
	
	void SetStart (int x, int z)
	{
		AddToSet(GridArr[x,z]);
	}
		
	// n is 'Next'
	void AddToSet (Transform n)
	{	
		Set.Insert (0, n);//Recursive
		Cell nScript = n.GetComponent<Cell> ();
		nScript.IsOpened = true;
	}
	
	void FindNext ()
	{
		
		if(Set.Count > 0){
			Transform previous = Set[0];
			Cell pScript = Set[0].GetComponent<Cell>();
		
			Transform next;
			Cell nScript;
				
			int prevX = (int)pScript.Position.x;
			int prevZ = (int)pScript.Position.z;
				
			int randSeed = Random.Range (0, 4);
			int counter = 0;
				
			int nextX;
			int nextZ;
				
			do {
				
				do {
									
					Vector3 randDirection = Directions [randSeed];
					
					randSeed = (randSeed + 1) % 4;
					
					nextX = prevX + (int)randDirection.x;
					nextZ = prevZ + (int)randDirection.z;
					
					counter++;
									
					if(counter > 4){
						AddToCompleteSet(previous);
						return;
					}
					
				} while(nextX < 0 || nextZ < 0 || nextX >= GridSize.x || nextZ >= GridSize.z);
				
				next = GridArr [nextX, nextZ];
				nScript = next.GetComponent <Cell> ();
							
			} while(nScript.IsOpened);
						
				ClearWalls(previous, next);
				AddToSet(next);
		}
		
	}

	void AddToCompleteSet(Transform toAdd){
		
		Set.Remove(toAdd);		
		CompletedSet.Add(toAdd);
				
		if(Set.Count == 0){
			
			Transform GetName = CompletedSet[0].transform;
			
			// Set the finish
			Vector3 cord = CompletedSet[0].transform.position;
			float cx = (float)cord.x;
			float cz = (float)cord.z;
					
			Transform[] allChild = GetName.GetComponentsInChildren<Transform>();
			
			foreach (Transform child in allChild) {
				
				if(child.Find("A") == null){
					
					Transform Exit;
					Exit = (Transform)Instantiate (ExitPrefab, new Vector3 (cx + 1f, 5.5f, cz - 1f), Quaternion.Euler(new Vector3(0f, 0f, 0f)));
					Exit.name = "Exit";
					break;
					
				}
				
				if(child.Find("B") == null){
					
					Transform Exit;
					Exit = (Transform)Instantiate (ExitPrefab, new Vector3 (cx - 1f, 5.5f, cz - 1f), Quaternion.Euler(new Vector3(0f, 90f, 0f)));
					Exit.name = "Exit";
					break;
					
				}
				if(child.Find("C") == null){
					
					Transform Exit;
					Exit = (Transform)Instantiate (ExitPrefab, new Vector3 (cx - 1f, 5.5f, cz + 1f), Quaternion.Euler(new Vector3(0f, 180f, 0f)));
					Exit.name = "Exit";
					break;
					
				}
				if(child.Find("D") == null){
					
					Transform Exit;
					Exit = (Transform)Instantiate (ExitPrefab, new Vector3 (cx + 1f, 5.5f, cz + 1f), Quaternion.Euler(new Vector3(0f, 270f, 0f)));
					Exit.name = "Exit";
					break;
					
				}

			}
			
		}
	
	}
	
	// 'p' Previous
	// 'n' Next
	void ClearWalls(Transform p, Transform n){
		
		RaycastHit[] hitInfo;
		hitInfo = Physics.RaycastAll(p.position + Vector3.up, n.position - p.position, Buffer);
		foreach(RaycastHit hit in hitInfo){
			Destroy(hit.transform.gameObject);
		}
		
	}
		
	void Update(){
		
		InvokeRepeating("FindNext", 0, 1.0f);
		
		if (Input.GetKeyDown (KeyCode.Tab) || Input.GetKey (KeyCode.Space)) {
			
			InvokeRepeating("FindNext", 0, 1.0f);
			
		}
		
		if (Input.GetKeyDown (KeyCode.LeftShift)) {
			
			FindNext();
			
		}
		
		if(Input.GetKeyDown(KeyCode.F1))
		{
					
			Application.LoadLevel(0);
			
		}
		
	}
	
}

The Exit.cs script:

using UnityEngine;
using System.Collections;

public class Exit : MonoBehaviour {
		
    void OnTriggerEnter(Collider other){
      
		if(GameObject.FindGameObjectWithTag("Player")){
            		
			CameraFade.StartAlphaFade( Color.black, false, 1f, 0.01f, () => { Application.LoadLevel(0); } );
					
		}
    
	}
		
}

My idea is to load the next level with GridSize + 1 and set the level number. The GridSize is set to Vector3(5,0,5) in the Inspector.

Cheers

Neil.

Maybe you could use PlayerPrefs to get the gridsize and levelnumber on start and set them on exit.
After you saved them you could reload the scene.

Thank you Arterie. I’ll have a look into this PlayerPrefs sounds like just what I’m after. :smile: