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.

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
`

Can you post the error from the console?

@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.

I think you should look there : Unity - Scripting API: Selection.SetActiveObjectWithContext to change the selection in the hierarchy.

And use

#if UNITY_EDITOR 
    //  your code using UnityEditor
#endif

to avoid error on build

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 Max 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.