• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by XxTheKi11erzxX · Jun 16, 2017 at 08:09 AM · scripting problembuild-error

Build error: The type or namespace name 'UnityEditor' could not be found.

When making my game, I use button, ads, etc. When I try to build this game it says "The type or namespace name 'Unity Editor' could not be found." I have all of them referencing Unity Editor, and I cannot put them into a Editor folder because these scripts make the game work! it happens with all my scripts and I have no idea what to do please Help!

ADS SCRIPT: https://pastebin.com/WwN96MNx

TOUCHABLE SCRIPT: https://pastebin.com/1Wn7qZ1K

PP SCRIPT: https://pastebin.com/qkCKWzga

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Jun 16, 2017 at 08:48 AM

First of all you can not use or reference anything from the UnityEditor namespace in a runtime script. If you do you have to ensure to wrap everything related to the UnityEditor namespace in pre-processor tags so it's not included at runtime.

I don't see any reason why your "Ads" script would require UnityEditor.Advertisement so just remove it.

Your "Touchable.cs" contains both, runtime code and editor code. This makes no sense. You have to move the Editor code into a seperate file which you place in an editor folder. The editor code can't be executed at runtime and has no effect on the runtime class.

 // Touchable.cs
 using UnityEngine;
 namespace UnityEngine.UI {
  
     public class Touchable : Graphic {
  
         public override bool Raycast(Vector2 sp, Camera eventCamera) {
             return true;
         }
  
         protected override void OnPopulateMesh(VertexHelper vh) {
             vh.Clear();
         }
     }
 }

This goes into a seperate file which you place in an editor folder.

 // TouchableEditor.cs
 using UnityEngine;
 using UnityEditor;
 namespace UnityEngine.UI
 {
     [CustomEditor(typeof(Touchable))]
     public class TouchableEditor : Editor
     {
         public override void OnInspectorGUI() {}
     }
 }

Finally your "pp" script uses an editor class to change the current scene. Why is that method "changeScene" actually inside a runtime class? Since you wrapped it in pre-processor tags the method isn't included in your build anyways. So it only works inside the editor. If you really need this you either have to wrap the using UnityEditor; and using UnityEditor.SceneManagement; as well in preprocessor tags so it's not included in your build. However in cases where you only need to use a single editor class you better remove the using statement and use the full class name:

 using UnityEngine;
 
 public class pp : MonoBehaviour
 {
     #if UNITY_EDITOR
     public void changeScene (int cs)
     {
         UnityEngine.SceneManagement.EditorSceneManager.LoadScene(cs);
     }
     #endif
 }

However if you actually want to be able to change the level at runtime you just want to use UnityEngine.SceneManagement.SceneManager.LoadScene(sceneIndex); which is a runtime class.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by tanoshimi · Jun 16, 2017 at 08:48 AM

Classes in the UnityEditor namespace are for use in editor scripts only.

When you create a build you need to remove the reference to that namespace and rewrite any code that uses classes from it.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to use Unity XR-Plugin Management from script/commandline 0 Answers

Why can't I build Web Player in Unity while I have no problems with building standalone versions? 2 Answers

My script behave different when I compile, idk why, everything was recorded 0 Answers

Why is this variable not setting in build? 0 Answers

How can I stop the variable values reverting to its default values after being built? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges