Scripting help to noob

I’m really new and don’t know much about code, however I’m wanting to learn. I have a basic game with unity 5.3.2 and for the life of me can’t find anything to help me add scripting in to load between scenes while already in a scene. If someone could help me with a very basic line of code (including anything that might needed to be added at the top) if be forever grateful. I’ve tried using scene manager, and application.loadlevel however still nothing. Please very basic step by step to load a scene when I press the escape key.

public class ExampleClass : MonoBehaviour {
  void Update() {
  if (Input.GetKey(KeyCode.Escape))
  {
  Application.LoadLevel("MyLevel");
  }
}

I think that should do what your asking (Explanation of code in spoiler tag):

Explain

Update() is called a function (Or more accurately being part of a class, a method - but nevermind that :)). It has ‘void’ in front of it because it is not meant to return any value. You can call functions whatever you want (almost). Update() is what is known as a keyword in that the Unity game engine already has something ‘pre built’ into it so it knows what to do whenever you use that keyword. In the case of Update(), it will execute the code within that function every frame in game.

{ and } basically defines all the code you want to be executed following a particular statement.

Input is what is known as a class, and GetKey is a method of that class. When you put your KeyCode.Escape brackets you are passing what is known as an argument into the parameters of that method.

Application is also a class, and LoadLevel is a method within the class Application.

That should be a good theoretical breakdown of what is going on in that code, if you should ever want it as a reference - don’t be intimidated if you don’t understand most of it at the moment, it will all come in time :).

Where I put “MyLevel”, put the name of the scene you want to load – don’t forget to include the quotation marks.

One other thing - you will need to add any scenes you want to load using LoadLevel into your game build, somewhere in File → Build Settings, I can’t remember exactly.

Application.LoadLevel() is obsolete. Use this instead:
http://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

publicclass LoadMenu : MonoBehaviour {

void update(){
if(Input.GetKey (KeyCode.K)){
SceneManager.LoadScene (“Level Manager”);
}
}
}

That is the current script im using, trying to hit K on my keyboard and bring me back to my Level Manager scene. when I hit the play button it does nothing, and even when I build my game and run it does nothing… Please help? am I using the wrong Input.GetKey or Input.GetButton or do I need the GetKeyDown or GetButtonDown or what. I’m awfully confused. I’d appreciate any help

Can you use code tags? It’s hard to read when you paste it to the normal forum.

1 Like

Do you have errors in the Console?

Update not update

capitalisation is important

1 Like