GameObject Constructor

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)); } }

Did you find this page useful? Please give it a rating: