Version: Unity 6 Preview (6000.0)
LanguageEnglish
  • C#

GameObject Constructor

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

Switch to Manual

Declaration

public GameObject();

Declaration

public GameObject(string name);

Declaration

public GameObject(string name, params Type[] components);

Parameters

name The name of the GameObject, specified as a string. The name is stored in the name property of the GameObject.
components The components to attach, specified as an array of types that inherit from Component.

Description

Creates a new GameObject, with optional parameters to specify a name and set of components to attach.

Use the constructor with no arguments to create a GameObject with an empty name property and only a Transform component attached.

Use the constructor with name parameter to create a GameObject with the specified value as the name property and only a Transform component attached.

Use the constructor with name and components parameters to create a GameObject with the specified name and the specified components attached, in addition to the Transform component.

using UnityEngine;

public class Example : MonoBehaviour { private void Start() { GameObject exampleOne = new GameObject(); exampleOne.name = "GameObject1"; exampleOne.AddComponent<Rigidbody>();

GameObject exampleTwo = new GameObject("GameObject2"); exampleTwo.AddComponent<Rigidbody>();

GameObject exampleThree = new GameObject("GameObject3", typeof(Rigidbody), typeof(BoxCollider)); } }