Search Unity

Create and destroy class object at runtime?

Discussion in 'Scripting' started by Ts1nd, Mar 23, 2021.

  1. Ts1nd

    Ts1nd

    Joined:
    Jan 24, 2021
    Posts:
    108
    I need to have dynamic class objects with 4 numeric values such as:
    Object1.var1, Object1.var2 etc

    The trick is I want the "Object1" to be dinamically created and deleted and also have its name to be dynamic. So I am looking for a way to create object from a given class at runtime like say a function :
    CreateObject(name, var1, var2, var3, var4) and also have a way to destroy it. I need it to show in inspector the same way as if I declared it as public in code... Is it possible?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Classes derived from ScriptableObjects can be edited in the editor.

    ScriptableObjects are just MonoBehaviors that can't be attached to GameObjects, so simple bags of data.

    You can make them at runtime with:

    https://docs.unity3d.com/ScriptReference/ScriptableObject.CreateInstance.html

    Or traditionally make them in advance.

    Go check out some tutorials on using them and their lifecycle. They can exist on disk or exist ephemerally only at runtime.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You can also do this with plain old C# objects, as long as they are [Serializable] and contain only serializable fields:

    Code (CSharp):
    1. using System;
    2.  
    3. [Serializable]
    4. public class MySerializableClass {
    5.     public string Name;
    6.     public int Var1;
    7.     public int Var2;
    8.     public float Var3;
    9.     public float Var4;
    10.  
    11.     public MySerializableClass(string name, int var1, int var2, float var3, float var4) {
    12.         this.Name = name;
    13.         this.Var1 = var1;
    14.         this.Var2 = var2;
    15.         this.Var3 = var3;
    16.         this.Var4 = var4;
    17.     }
    18. }
    Now you can declare fields of this type in your MonoBehaviours and they will show in the inspector:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MyTestMonoBehaviour : MonoBehaviour {
    4.     public MySerializableClass Test;
    5. }
     
    Kurt-Dekker likes this.
  4. Ts1nd

    Ts1nd

    Joined:
    Jan 24, 2021
    Posts:
    108
    This is totally not what I wanted. I know we can define things like that, the problem is you need to do it in code like you did in your example with Test variable. I want this Test to be defined in game (including its name) and changed/deleted dynamically
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    I have a package called Datasacks that is generally for UI interoperability, but you can make Datasacks as much as you want, and use them as blobs of data in your game.

    Here's how it works:

    20180724_datasacks_overview.png
    Each one is just a ScriptableObject. The normal use is to make them in advance as assets, but you can make them on the fly.

    Allowing editing them on the fly in the finished game would just be a trivial UI operation, since these things are in fact intended to interoperate UnityEngine.UI objects in the first place.

    Datasacks is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/datasacks

    https://github.com/kurtdekker/datasacks

    https://gitlab.com/kurtdekker/datasacks

    https://sourceforge.net/projects/datasacks/
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    I don't understand what you mean by defined in game.