Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


CreateGameObject

Creates a new GameObject with the specified name and components, in a specific scene and with hide flags.
Read time 1 minuteLast updated 4 days ago

Definition

  • Type: Method
  • Namespace: UnityEditor
  • Assembly: UnityEditor.CoreModule

CreateGameObject(Scene, HideFlags, string, Type[])

Creates a new GameObject with the specified name and components, in a specific scene and with hide flags.
public static GameObject CreateGameObject(Scene scene, HideFlags hideFlags, string name, params Type[] types)

Parameters

scene

The scene to create the GameObject in.

hideFlags

The hide flags to assign to the GameObject.

name

The name of the new GameObject.

types

The types of the components to add to the new GameObject.

Returns

Type

Description

GameObjectThe GameObject that the method created.

Remarks

The method throws an exception if
scene
isn't valid.

CreateGameObject(string, Type[])

Creates a new GameObject with the specified name and components.
public static GameObject CreateGameObject(string name, params Type[] types)

Parameters

name

The name of the new GameObject.

types

The types of the components to add to the new GameObject.

Returns

Type

Description

GameObjectThe GameObject that the method created.

Remarks

The method adds the components while the GameObject is inactive, then activates it and gives it the default parent for new objects. To create the GameObject in a specific scene, use the overload that takes a Scene.

Examples

using UnityEngine;using UnityEditor;public class CreateComponentExample{ [MenuItem("ObjectFactoryExample/Create Camera GameObject")] public void CreateCameraEditor() { Selection.activeGameObject = ObjectFactory.CreateGameObject("Camera", typeof(Camera)); }}