- Home /
The question is answered, right answer was accepted
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.
What exact error are you getting? I think that shouldn't fail. Are you sure the error isn't in your GameObject.Find("").GetComponent() ?
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
Add using UnityEditor; in the script header. Also maybe edit your question to reflect this.
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;
@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.
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.
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.
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.
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
I put his in and VS barks 'Using directive is unnecessary'.
Yeah it is unnecessary, because you wrote the full name "UnityEditor.Selection".
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.
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?
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.
@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.
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 :-)
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!
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.
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
Follow this Question
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