Converting Objects to GameObjects (Javascript)

Hi!
I have a javascript array of GameObjects (its public) and I am trying to access one to turn it active. However, it is showing up as an Object, not a GameObject, and I can’t convert it. Here’s my code:

(new GameObject(AlignedItems[Holding])).SetActive(true);

AlignedItems is the array, and Holding is an int. If I do this, it says:

InvalidCastException: Cannot cast from source type to destination type.

The GameObject ctor can only take a string as first parameter, or nothing. You are trying to assign Object or GameObject while string is none of those (it is object but not UnityEngine.Object).

Your solution could be you want the name of that GameObject in the collection:

(new GameObject(AlignedItems[Holding].name)).SetActive(true);

Just assuming as I do not know what you wish to pass. Still your issue is that you are trying to pass the wrong type of item.

If you have a GameObject, you can call SetActive() on it directly. You don’t need to wrap it in new GameObject(). The error you get is probably because GameObject does not have a constructor that takes another GameObject. That said, I tried to reproduce your case using the following script:

#pragma strict

function Start () {
    var gameObjects = [new GameObject()];
    (new GameObject(gameObjects[0])).SetActive(true);
}

but I got a different error:

BCE0024: The type ‘UnityEngine.GameObject’ does not have a visible constructor that matches the argument list ‘(UnityEngine.GameObject)’.

so perhaps I misunderstood your use case. Feel free to post a little more of your script if my suggestion doesn’t work.