Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug I can't compile my game but only can playtest it.

Discussion in 'Scripting' started by PuroTheFriend, Apr 9, 2024.

  1. PuroTheFriend

    PuroTheFriend

    Joined:
    Jun 23, 2021
    Posts:
    14
    I can't compile my game anymore from this stupid 2 errors.

    I tried upgrading my unity, it didn't work, i don't want to start over.

    UnityEditor.BuildPlayerWindow+BuildMethodException: 2 errors
    at UnityEditor.BuildPlayerWindow+DefaultBuildMethods.BuildPlayer (UnityEditor.BuildPlayerOptions options) [0x002da] in <8a172039c50c4113a07e608ee3976ae2>:0
    at UnityEditor.BuildPlayerWindow.CallBuildMethods (System.Boolean askForBuildLocation, UnityEditor.BuildOptions defaultBuildOptions) [0x00080] in <8a172039c50c4113a07e608ee3976ae2>:0
    UnityEngine.GUIUtility:processEvent (int,intptr,bool&)

    upload_2024-4-9_18-55-32.png
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,410
    Always look at the very first error that appears!

    Your console shows three errors but there ought to be more above "error building player because scripts had compiler errors" because those compile errors will show up in the console too. Those are the ones you need to show / fix. And please post them as text.
     
    Ryiah likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,005
    Neither of those things are how you fix errors. :)

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.


    You may only use
    UnityEditor
    namespace methods and classes while you are in the Unity editor.

    You may never use anything from the
    UnityEditor
    namespace in a build.

    You can fix this in one of two ways:

    - wrap these bits of editor code in
    #if UNITY_EDITOR
    conditional compilation directives

    - you can put the entire script into a folder called
    Editor
    . This makes it what is loosely called an "editor script."

    Such "editor scripts" can NEVER be attached to GameObjects, either in prefabs or scenes.
     
  4. PuroTheFriend

    PuroTheFriend

    Joined:
    Jun 23, 2021
    Posts:
    14
    The last thing i added before this happened is the "using UnityEditor;" code to make the select file dialog appear, is there some way to replace the unityeditor's file picker with the other one?
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,153
    Anything in the UnityEditor namespace is, as the name implies, only available in the editor. Generally any 'Editor' namespaces from Unity's side or other packages is only for use in the editor. It can't be used in a built project.

    You can of course use the code for editor related things. Though you'll need to ensure the code is in an Editor folder, or surrounded with
    #if UNITY_EDITOR
    pre-processors, or in an editor-only assembly definition.

    For file select dialogues, you may need to look at the asset store for that.
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,448
    spiney199 likes this.
  7. PuroTheFriend

    PuroTheFriend

    Joined:
    Jun 23, 2021
    Posts:
    14
  8. PuroTheFriend

    PuroTheFriend

    Joined:
    Jun 23, 2021
    Posts:
    14
    Alright! Basic stuff figured out, Now the goal for is to display the image once pressed submit.
     
  9. PuroTheFriend

    PuroTheFriend

    Joined:
    Jun 23, 2021
    Posts:
    14
    I guess i should stop working on the level editor since it doesn't even work.

    Unless someone fixes it.

    Code (CSharp):
    1. using System;
    2. using System.IO;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7.  
    8. public class LevelDesignerPreview : MonoBehaviour
    9. {
    10.     public bool isEditor;
    11.  
    12.     public int scalex;
    13.     public int scaley;
    14.  
    15.     public GameObject scalexinput;
    16.     public GameObject scaleyinput;
    17.  
    18.     public string backgroundpath;
    19.  
    20.     public GameObject background;
    21.     public SpriteRenderer backgroundspriteRenderer;
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.         if (isEditor == false)
    27.         {
    28.             string path = PlayerPrefs.GetString("designbackground");
    29.  
    30.             if (!string.IsNullOrEmpty(path) && File.Exists(path))
    31.             {
    32.                 // Background
    33.                 Texture2D texture = new Texture2D(1, 1);
    34.                 byte[] data = File.ReadAllBytes(path);
    35.                 texture.LoadImage(data);
    36.  
    37.                 Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
    38.  
    39.                 backgroundspriteRenderer.sprite = sprite;
    40.  
    41.                 // Adjustment
    42.                 scalex = PlayerPrefs.GetInt("designbackgroundscalex");
    43.                 scaley = PlayerPrefs.GetInt("designbackgroundscaley");
    44.  
    45.                 background.transform.localScale = new Vector3(scalex, scaley, 1.0f);
    46.             }
    47.         }
    48.     }
    49.  
    50.     // Update is called once per frame
    51.     void Update()
    52.     {
    53.        
    54.     }
    55.  
    56.     public void TestDesign()
    57.     {
    58.         PlayerPrefs.SetInt("designbackgroundscalex", scalex);
    59.         PlayerPrefs.SetInt("designbackgroundscaley", scaley);
    60.         PlayerPrefs.SetString("designbackground",backgroundpath);
    61.         PlayerPrefs.Save();
    62.     }
    63.  
    64.     public void OpenFileDialog()
    65.     {
    66.         string path = UnityEditor.EditorUtility.OpenFilePanel("Select Image", "", "png,jpg");
    67.  
    68.         if (!string.IsNullOrEmpty(path) && File.Exists(path))
    69.         {
    70.             Texture2D texture = new Texture2D(1, 1);
    71.             byte[] data = File.ReadAllBytes(path);
    72.             texture.LoadImage(data);
    73.  
    74.             Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
    75.             backgroundspriteRenderer.sprite = sprite;
    76.  
    77.             backgroundpath = path.ToString();
    78.         }
    79.     }  
    80.  
    81.     public void ScaleChanged()
    82.     {
    83.         scalex = int.Parse(scalexinput.GetComponent<InputField>().text);
    84.         scaley = int.Parse(scaleyinput.GetComponent<InputField>().text);
    85.  
    86.         background.transform.localScale = new Vector3(scalex, scaley, 1.0f);
    87.     }
    88. }
    89.  
     
  10. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,448
    Last edited: May 15, 2024 at 12:50 PM
    Kurt-Dekker and Spy-Master like this.