CreateInstance
Creates an instance of a scriptable object.
Read time 1 minuteLast updated 5 days ago
Definition
- Type: Method
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
CreateInstance(string)
Creates an instance of a scriptable object.
public static ScriptableObject CreateInstance(string className)
Parameters
The type of the ScriptableObject to create, as the name of the type.
Returns
Type | Description |
|---|---|
| ScriptableObject | The created ScriptableObject. |
Remarks
To easily create a ScriptableObject instance that is bound to a .asset file via the Editor user interface, consider using CreateAssetMenuAttribute.
Examples
using UnityEngine;public class MyData : ScriptableObject{ public int value;}public class CreateInstance : MonoBehaviour{ void Start() { ScriptableObject nonGenericInstance = ScriptableObject.CreateInstance(typeof(MyData)); ((MyData)nonGenericInstance).value = 20; Debug.Log("Non-generic instance value: " + ((MyData)nonGenericInstance).value); }}
CreateInstance(Type)
Creates an instance of a scriptable object.
public static ScriptableObject CreateInstance(Type type)
Parameters
The type of the ScriptableObject to create, as a System.Type instance.
Returns
Type | Description |
|---|---|
| ScriptableObject | The created ScriptableObject. |
Remarks
To easily create a ScriptableObject instance that is bound to a .asset file via the Editor user interface, consider using CreateAssetMenuAttribute.
Examples
using UnityEngine;public class MyData : ScriptableObject{ public int value;}public class CreateInstance : MonoBehaviour{ void Start() { ScriptableObject nonGenericInstance = ScriptableObject.CreateInstance(typeof(MyData)); ((MyData)nonGenericInstance).value = 20; Debug.Log("Non-generic instance value: " + ((MyData)nonGenericInstance).value); }}
CreateInstance<T>()
Creates an instance of a scriptable object.
public static T CreateInstance<T>() where T : ScriptableObject
Returns
Type | Description |
|---|---|
| T | The created ScriptableObject. |
Remarks
To easily create a ScriptableObject instance that is bound to a .asset file via the Editor user interface, consider using CreateAssetMenuAttribute.
Examples
using UnityEngine;public class MyData : ScriptableObject{ public int value;}public class CreateInstanceGeneric : MonoBehaviour{ void Start() { MyData genericInstance = ScriptableObject.CreateInstance<MyData>(); genericInstance.value = 10; Debug.Log("Generic instance value: " + genericInstance.value); }}