The programm runs fine in the Unity editor but doesnt work after build & run

Unity Version 5.6.5f1
Issue:
Can’t build my game

Error:
Error building Player because scripts have compile errors in the editor

Code:

using System.Collections;
using System.Collections.Generic;
#if UNITY_EDITOR

using UnityEngine;
using UnityEngine.UI;
using UnityEditor.SceneManagement;
#endif

public class LevelManager : MonoBehaviour {

	public void startgame (string name){
		EditorSceneManager.LoadScene (name);
	} 

	public void optionsLoad (string name){
		EditorSceneManager.LoadScene (name);
	} 

	public void doExitGame() {
		Application.Quit();
		Debug.Log ("I Quit THE GAME NOW EHEHEH");
	}
}

Hi fabio,

What are the errors you are receiving, can you post a screenshot please?

Also, did you install the WebGL build support separately by downloading it from the Unity website?

Incidentally, why are you using the #if UNITY_EDITOR statements? What build platform are you targetting?


See also;

Continuing the discussion from The programm runs fine in the Unity editor but doesnt work after build & run:

Hello Rob,

I just searched up the problem on goolge and some ppl use that to build and run it i dont know exactly what it does. the platform is Windows and no i didnt install the webgl build support . screenshot here https://i.imgur.com/tS4dPC2.png

Thanks for the help i really appreciate it

Hi, thanks for the screenshot and info.

Try the following then;

  • check the Build Settings and be sure that all of your scenes are included

  • within LevelManager remove these lines;

    #if UNITY_EDITOR
    
    #endif
    
  • replace this line;

    using UnityEditor.SceneManagement;
    

    with this;

    using UnityEngine.SceneManagement;
    
  • replace instances of this (in your startgame and optionsLoad methods);

    EditorSceneManager.LoadScene(name);
    

    with this;

    SceneManager.LoadScene(name);
    

Note, you can just copy/paste your images into your posts on the forum also :slight_smile:

Hey Rob,

It really worked thank you very much, can you explain to me what the issue was?
because i dont really understand it i think it could have been that i used the EditorScenemanager instead of the enigne scene manager but why was that a problem? thanks anyway :smiley:

Greetings Fabio

Hi Fabio,

Of course, I will be happy to.

These lines;

#if UNITY_EDITOR
  // ....
#endif

are basically saying, “if the platform I am running on is the Unity editor, include these following lines”.

So, in your case, you were only using the three using directives in between if the platform was Unity. When you did your build, they would have been ignored. As such, anything that relied upon those libraries/classes would have failed to be recognised by the compiler/loaded.

The next issue was that you have specified the UnityEditor.SceneManagement directive, again, this will work within Unity, but if you are trying to run the game on another platform you still need the ability to load your scenes.

Note, all of these things were not in the course, as such I would, at least for now, follow the course more closely and you will avoid these issues. Then, gradually, expand on the functionality of each game by trying different things at that point.

On a related note, you would be doing yourself a big favour if you stick to naming conventions also, for example, class names and methods typically use PascalCase in C#, and variables camelCase.

So the methods you have, startgame, optionsLoad and doExitGame would be better written as;

StartGame, OptionsLoad and DoExitGame

Then, consider naming them specifically for what they do, for example LoadOptions and ExitGame.

This will not only make your code easier to follow for yourself, but if you should happen to work with others, or just share your code online when asking for some help, it makes it a lot easier for everyone else also :slight_smile:

Hope this helps :slight_smile:

1 Like

Privacy & Terms