Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Nov 26, 2018 at 09:20 AM by pako for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by giantkilleroverunity3d · Nov 21, 2018 at 07:58 PM · c#gameobjectselectioneditor scripting

Why do I get error on the Selection.activeGameObject = gameObject?

First line in Start() was correct in Javascript and from what I have seen it is here unless I have to declare into a variable first?
So I did declare into a null variable.
And the other public vars are set in the inspector. But this line should select the GameObject it is on. I am still converting this from JS to C.

 //using Ludiq;
 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using System.Collections.Generic;
 
 //[IncludeInSettings(true)]
 public class VROGOHalo : MonoBehaviour
 
 {
     //************************************************************************
     // These items show up in the inspector
     //************************************************************************
     public Transform target = null;
     public Camera PC = null;
     public float distance = 10.0f;
 
     public float xSpeed = 75.0f; //Pulsar rotation speed 250
     public float ySpeed = 75.0f; //Pulsar rotation speed 120
 
     //**********************************
     //Mouse cursor window parameters
     //**********************************
     public float xMinLimit = -20f;
     public float xMaxLimit = 80f;
     public float yMinLimit = -20f;
     public float yMaxLimit = 80f;
     public float yWindow = 2f; // Mouse Window edges
     public float xWindow = 2f; // Mouse Window edges
 
     //**********************************
     // These items are available only in code
     //**********************************
     static public float xrotation = 0.0f;
     static public float yrotation = 0.0f;
     static public float xread = 0.0f;
     static public float yread = 0.0f;
     static public float xhbase = 5f; // Mouse Window edges
     static public float xlbase = 5f; // Mouse Window edges
     static public float yhbase = 5f; // Mouse Window edges
     static public float ylbase = 5f; // Mouse Window edges
 
     static public float windowBoundary = 3; //window boundaries
     static public float xhigh = windowBoundary;
     static public float xlow = windowBoundary;
     static public float yhigh = windowBoundary;
     static public float ylow = windowBoundary;
     static public float csx = 0.0f;
     static public float csy = 0.0f;
     static public float Pulsarx;
     static public float Pulsary;
     static public float Pulsarz;
 
     //private float z = 0.0f; //mod
 
     //@script AddComponentMenu("Camera-Control/Mouse halo")
     [AddComponentMenu("Camera-Control/Mouse halo")]
 
     //****************************************************************
     //****** Routines start here *************************************
     //****************************************************************
     //****************************************************************
 
     public void Start()
     {
         UnityEditor.Selection.activeGameObject = gameObject;
         //Selection.activeGameObject = gameObject; //This selects the hierarchy object
         Debug.Log("Made it to Start");
         PC = GameObject.Find("").GetComponent<Camera>(); // The default is Camera
         if (!PC) {
             Debug.Log("Camera not found");
             return;
         }
         PC.transform.parent = transform; //This puts the Camera as child with parent!!!
 
         Shotaim_neuter(); //Initializes variable at this point
         CuebStatic();
         //z = angles.z; // mod
 
         // Make the rigid body not change rotation
         //if (rigidbody)
         //    rigidbody.freezeRotation = true;
     }

Thank you in advance. Months, weeks and days spent on this... I keep dabbling in it trying to fix this with out asking for help. I want to learn this on my own, but this is a brick wall to me.

Comment
Add comment · Show 17
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 ecv80 · Nov 21, 2018 at 09:12 PM 0
Share

What exact error are you getting? I think that shouldn't fail. Are you sure the error isn't in your GameObject.Find("").GetComponent() ?

avatar image giantkilleroverunity3d ecv80 · Nov 21, 2018 at 09:53 PM 0
Share

error CS0103: The name `Selection' does not exist in the current context //This selects the hierarchy object is the comment. First line in Start(); The error points to that line of code

avatar image ecv80 giantkilleroverunity3d · Nov 21, 2018 at 10:03 PM 0
Share

Add using UnityEditor; in the script header. Also maybe edit your question to reflect this.

Show more comments
Show more comments
avatar image Happeloy · Nov 21, 2018 at 09:20 PM 1
Share

The GameObject.Find("") doesn't look right, ecv80 also said. If the camera you want to get is your main camera, and tagget as mainCamera, you can use Camera.main ins$$anonymous$$d.

You said that you don't know if you need to declare a variable first. Is PC not declared? It would have to be declared as Camera PC;

avatar image ThePixelatedSword · Nov 22, 2018 at 10:56 AM 0
Share

Can you post the error from the console?

avatar image pako · Nov 22, 2018 at 10:57 AM 0
Share

@giantkilleroverunity3d I don't have experience with UnityEditor.Selection.activeGameObject so I'm answering after reading the relevant documentation.


error CS0103: The name Selection' does not exist in the current context //This selects the hierarchy object is the comment. First line in Start(); The error points to that line of code


It seems to me that UnityEditor.Selection.activeGameObject is readonly and you are trying to write to it. I think it just returns the selected gameObject, and that you can't actually assign to it. The documentation for activeGameObject says:

Returns the active game object. (The one shown in the inspector).

So, maybe I'm wrong, but this seems to be readonly.


Also see the Reply on this post.

avatar image ThePixelatedSword pako · Nov 22, 2018 at 11:12 AM 1
Share
 UnityEditor.Selection.activeGameObject

is both readable and writable. I can't reproudce this error with UnityEditor.Selection.activeGameObject = gameobject; It compiles with no errors for me.

avatar image pako ThePixelatedSword · Nov 22, 2018 at 12:39 PM 0
Share

Quit right! I tested it out and it works just fine writing to it. In fact having the statement that produces the error: UnityEditor.Selection.activeGameObject = gameObject as the only statement inside Start() works perfectly well. I guess the link I mentioned saying that writing to activeGameObject is buggy must be outdated.


I'll try to reproduce the problem in my testbed.

Show more comments
Show more comments
avatar image pako · Nov 22, 2018 at 02:17 PM 0
Share

There's no way I can reproduce this issue. There are two errors reported CS0234 and CS0103, both of which point to the UnityEditor.Selection class not being available. I even tried removing the references to UnityEditor from all the Assemblies in the VS Solution Explorer, deleted the assembly files from the project folder, deleted the obj and Library folders from the project folder, but after I open Unity again everything, including the references to UnityEditor is rebuilt automatically, and everything runs fine!


So, @giantkilleroverunity3d the only things that I can think of at this point, is to start a new empty project, copy the VROGOHalo script there, and make some test runs. If that doesn't work, maybe you should uninstall and reinstall Unity.


Since I can't reproduce the issue, I'm afraid I can't provide any more help.

2 Replies

  • Sort: 
avatar image
4
Best Answer

Answer by MisteryJ · Nov 22, 2018 at 11:36 AM

Do you get an error when building the game? UnityEditor cannot be used in Monobehaviour, because the build does not contain the UnityEditor library. If you want to use it in the editor, you can add

 
#if UNITY_EDITOR 
    using UnityEditor;
#endif
////
#if UNITY_EDITOR 
    //  your code using UnityEditor
#endif

Comment
Add comment · Show 9 · 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 giantkilleroverunity3d · Nov 22, 2018 at 07:05 PM 0
Share

I put his in and VS barks 'Using directive is unnecessary'.

avatar image MisteryJ giantkilleroverunity3d · Nov 22, 2018 at 07:59 PM 0
Share

Yeah it is unnecessary, because you wrote the full name "UnityEditor.Selection".

avatar image ecv80 · Nov 22, 2018 at 09:15 PM 0
Share

Is it an editor script you're doing or a game? Because... well, not sure about $$anonymous$$onoBehaviour but what $$anonymous$$isteryJ said. Can't use this in a build.

avatar image giantkilleroverunity3d ecv80 · Nov 23, 2018 at 05:31 AM 0
Share

I am building a game. It is an Oculus/Android apk. But I really want to understand this error being thrown during the build process. If I remove it the VS throws the error. The VROGOHalo.cs is in Camera scripts but not used anywhere as of yet. Could this be what throws the error because it has no selections made on the public variables?

avatar image ecv80 giantkilleroverunity3d · Nov 23, 2018 at 07:13 AM 0
Share

You're not supposed to use UnityEditor for games. It doesn't make sense anyways. If there's UnityEditor code you need to run during editor usage you can use the directives $$anonymous$$isteryJ said to avoid problems in the build.

avatar image pako · Nov 23, 2018 at 09:20 AM 1
Share

@giantkilleroverunity3d I'm quite confused with all that's been said, starting with your original post, where you say that this worked in JavaScript. Well, it's impossible this to have worked in JavaScript, because as @$$anonymous$$isteryJ and @ecv80 have already said, you can't have unconditionally compiled editor code in a build outside the editor. In other words, when you hit play in the editor the VROGOHalo.cs script should build and run just fine with no errors, but when you build an apk there will be compile errors, because the UnityEditor.dllis not included in any build outside of the editor environment.


So, maybe your code in Javascript worked well in the editor only? $$anonymous$$aybe you hadn't built an apk with Javascript? and then you converted to C# and tried to compile an apk, which resulted in compile errors? Everybody may do something like that sometimes, and this would make sense. It would also explain why I can't reproduce the issue, since I only tried in the editor.


avatar image pako · Nov 23, 2018 at 11:30 AM 0
Share

Tried again to reproduce the errors. I just have Selection.activeGameObject = gameObject inside Start() without any conditional compilation, i.e. without using a #if UNITY_EDITOR block, and with platform set to Android. This time I start an apk build, which of course fails, with error: "Error building Player because scripts had compiler errors".


So, no CS0234 and CS0103 errors as @giantkilleroverunity3d reports, which I believe should be thrown during an editor build.

Therefore, since I still can't reproduce these exact errors, I suspect that it might be a case of corrupted project or Unity installation, per one of my previous comments.


If someone else can reproduce the reported errors, please let me now how, since I've been busting my head over this for 2 days, and I need closure :-)

avatar image pako pako · Nov 23, 2018 at 11:55 AM 1
Share

Oops! my bad! There is a CS0103 error thrown, just before the "Error building Player because scripts had compiler errors". This was further up the console window and I missed it before. Sorry about that.

So, finally! issue reproduced! It's due to incorrect usage of editor code in a non-editor build. +1 to @$$anonymous$$isteryJ for being the first one to spot it!

avatar image giantkilleroverunity3d · Nov 25, 2018 at 07:36 PM 0
Share

From my position I didn't understand how deep the question was as compared to how I inadvertently got there. Just the same, I now have a better understanding of what I actually did in lieu of what I thought I was accomplishing. I was using a previous PC game project and just trying to push it as is to the Oculus GO to test the headset with my code. I now know better. I have some more work to do. The previous project also has some FBX and $$anonymous$$ax files in it of which I will use Wings3d to redo the objs. That way I truly own my own work.
Thank you all and I apologize for substantiating any headaches. It seems all my Unity efforts are this complex. The easy stuff I can do.

avatar image
0

Answer by FlavienM · Nov 23, 2018 at 07:51 AM

I think you should look there : https://docs.unity3d.com/ScriptReference/Selection.SetActiveObjectWithContext.html to change the selection in the hierarchy.

And use

 #if UNITY_EDITOR 
     //  your code using UnityEditor
 #endif

to avoid error on build

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

Follow this Question

Answers Answers and Comments

579 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 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 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 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 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 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

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How does Unity handle GameObject selection in the Scene View? 1 Answer

Disabling A Script on a GameObject From a Different Script 2 Answers

gameObject are not referenced 2 Answers


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