Version: Unity 7.0 Alpha (7000.0)
LanguageEnglish
  • C#

ObjectFactory.CreateGameObject

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

Declaration

public static GameObject CreateGameObject(string name, params Type[] types);

Parameters

Parameter Description
name The name of the new GameObject.
types The types of the components to add to the new GameObject.

Returns

GameObject The GameObject that the method created.

Description

Creates a new GameObject with the specified name and components.

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.

using UnityEngine;
using UnityEditor;

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

Declaration

public static GameObject CreateGameObject(Scene scene, HideFlags hideFlags, string name, params Type[] types);

Parameters

Parameter Description
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

GameObject The GameObject that the method created.

Description

Creates a new GameObject with the specified name and components, in a specific scene and with hide flags.

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