Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


CreateInstance

Creates a new instance of a serialized object of type T.
Read time 1 minuteLast updated 3 days ago

Definition

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

CreateInstance<T>()

Creates a new instance of a serialized object of type T.
public static T CreateInstance<T>() where T : Object

Returns

Type

Description

TThe new object, as type
T
.

Remarks

Use this method to create any type of serialized object in the Unity Editor. The new instance uses default values. This generic overload returns the object as type
T
, so you don't need to cast it.

CreateInstance(Type)

Creates a new instance of a serialized object of the specified type.
public static Object CreateInstance(Type type)

Parameters

type

The type of object to create.

Returns

Type

Description

ObjectThe new object.

Remarks

Use this method to create any type of serialized object in the Unity Editor. The new instance uses default values.

Examples

using UnityEngine;using UnityEditor;public class CreateInstanceExample{ [MenuItem("ObjectFactoryExample/Create Material Asset")] public void CreateMaterialEditor() { Material material = ObjectFactory.CreateInstance<Material>(); material.shader = Shader.Find("Transparent/Diffuse"); AssetDatabase.CreateAsset(material, "Assets/newMaterial.mat"); }}