- Home /
Enable GameObject in Hierarchy with Script
I have a problem and I can't find the solution, I hope you guys can help me out.
I am trying to enable a gameobject in the hierarchy.
I use this Javascript for it. I assign the meshes to the vars. and let the script pick randomly one of the 5 path-layouts for the puzzle. (so the gameobjects are by default disabled in the hierarchy) Now i want to enable them with this script. However it doesn't seem to work. So can someone check my script? I have the feeling there is something wrong with this line. Green.enabled(true);
//Mesh of the right Maze.Path.gameobject
var Green : GameObject;
var Red : GameObject;
var Blue : GameObject;
var Yellow : GameObject;
var Brown: GameObject;
//randomize the puzzle Layout
function RandomMaze()
{
var randMaze = Random.Range(0, 4);
var chosenLayout = randMaze;
switch (chosenLayout)
{
case 0:
Green.enabled(true);
break;
case 1:
Red.enabled(true);
break;
case 2:
Blue.enabled(true);
break;
case 3:
Yellow.enabled(true);
break;
case 4:
Brown.enabled(true);
break;
}
}
I appriciate your help. Thanks
I think that .enabled is only for components and .SetActive is for GameObject
Try this:
Green.activeinHierarchy = true;
Still, no change :( but i will keep trying to find the solution. Thx
Update : I am now looking at "https://docs.unity3d.com/Documentation/ScriptReference/GameObject.SetActive.html"
"Note that a GameObject may be inactive because a parent is not active. In that case, calling SetActive() will not activate it, but only set the local state of the GameObject, which can be checked using GameObject.activeSelf. This state will then be used once all parents are active."
Your answer
Follow this Question
Related Questions
How to deactivate something when holding down a key? 1 Answer
enable and disable boxcollider (whats wrong with my script?) 2 Answers
How to tell if 2 blocks are next to each other in a 2d game? 1 Answer
OnCollisionEnter Collision not detected? 1 Answer
Unity IDE: Finding out object type of arbitrary item in Hierarchy 3 Answers