Legacy Documentation: Version 5.5
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

EditorUtility.CreateGameObjectWithHideFlags

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static function CreateGameObjectWithHideFlags(name: string, flags: HideFlags, params components: Type[]): GameObject;
public static GameObject CreateGameObjectWithHideFlags(string name, HideFlags flags, params Type[] components);

Description

Creates a game object with HideFlags and specified components.

This is very similar to creating a GameObject the usual way, except it sets the specified HideFlags immediately.

Editor Window that shows how does the example looks.

#pragma strict
public class CreateGOHideFlagsExample extends EditorWindow {
	var objName: String = "GameObject";
	var instanceID: int = 0;
	var create: boolean = true;
	var go: GameObject = null;
	var hideHierarchy: boolean = false;
	@MenuItem("Example/GameObject Flags")
	static function Init() {
		// Get existing open window or if none, make a new one:
		var window: CreateGOHideFlagsExample = CreateGOHideFlagsExampleEditorWindow.GetWindow(CreateGOHideFlagsExample);
		window.Show();
	}
	function OnGUI() {
		create = EditorGUILayout.Toggle("Create a GO:", create);
		GUI.enabled = create;
		objName = EditorGUILayout.TextField("GameObject Name:", objName);
		if (GUILayout.Button("Create")) {
			var created: GameObject = EditorUtility.CreateGameObjectWithHideFlags(objName, hideHierarchy ? HideFlags.HideInHierarchy : 0);
			instanceID = created.GetInstanceID();
			Debug.Log("Created GameObject ID: " + instanceID);
		}
		GUI.enabled = !create;
		EditorGUILayout.BeginHorizontal();
		instanceID = EditorGUILayout.IntField("Instance ID:", instanceID);
		if (GUILayout.Button("Search & Update flags")) {
			go = null;
			go = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
			if (go)
				go.hideFlags = hideHierarchy ? HideFlags.HideInHierarchy : 0;
		}
		EditorGUILayout.EndHorizontal();
		if (!go)
			EditorGUILayout.LabelField("Object: ", (go == null) ? "No object was found" : go.name);
		GUI.enabled = true;
		hideHierarchy = EditorGUILayout.Toggle("HideInHierarchy", hideHierarchy);
	}
	function OnInspectorUpdate() {
		Repaint();
	}
}
using UnityEngine;
using UnityEditor;

public class CreateGOHideFlagsExample : EditorWindow { string objName = "GameObject"; int instanceID = 0; bool create = true; GameObject go = null; bool hideHierarchy = false;

[MenuItem( "Example/GameObject Flags" )] static void Init( ) { // Get existing open window or if none, make a new one: CreateGOHideFlagsExample window = (CreateGOHideFlagsExample)EditorWindow.GetWindow( typeof(CreateGOHideFlagsExample) ); window.Show( ); }

void OnGUI( ) { create = EditorGUILayout.Toggle( "Create a GO:", create ); GUI.enabled = create;

objName = EditorGUILayout.TextField( "GameObject Name:", objName ); if( GUILayout.Button( "Create" ) ) { GameObject created = EditorUtility.CreateGameObjectWithHideFlags( objName, hideHierarchy ? HideFlags.HideInHierarchy : 0);

instanceID = created.GetInstanceID( ); Debug.Log( "Created GameObject ID: " + instanceID ); }

GUI.enabled = !create;

EditorGUILayout.BeginHorizontal( );

instanceID = EditorGUILayout.IntField( "Instance ID:", instanceID );

if( GUILayout.Button( "Search & Update flags" ) ) { go = null; go = EditorUtility.InstanceIDToObject( instanceID ) as GameObject; if( go ) go.hideFlags = hideHierarchy ? HideFlags.HideInHierarchy : 0; }

EditorGUILayout.EndHorizontal( );

if( !go ) EditorGUILayout.LabelField( "Object: ", (go == null) ? "No object was found" : go.name );

GUI.enabled = true; hideHierarchy = EditorGUILayout.Toggle( "HideInHierarchy", hideHierarchy ); }

void OnInspectorUpdate( ) { Repaint( ); } }