Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Using classes in real project?

Discussion in 'Scripting' started by McPeppergames, Oct 27, 2022.

  1. McPeppergames

    McPeppergames

    Joined:
    Feb 15, 2019
    Posts:
    103
    I can find a lot of tutorial videos online about creating classes in Unity and C# to for example create a character class which contains a name, strength, health, etc.

    But I wonder how this kind of "class" is used in a real Unity project to create the "physical" objects and give this object the character values? How are this values accessed later by using the "physical" object?

    What is the best practice here?

    And how do I delete the "physical" object while making sure all the values (class) object also gets destroyed and removed from the memory? (so all memory usage is cleaned up)

    All help welcome!

    Thank you!
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,612
    All Unity assets are represented by, two, I suppose actually three different things. There's the file on your computer, ergo, the serialised data. Then there's the C# class you use to interact with it through the Unity API, and then every Unity asset is also represented by a C++ object on Unity's internal side of things (known as the managed side).

    Memory management in C# is mostly automatic with the garbage collector. In the C++ side that you don't touch unless you have source access, Unity handles everything. All you really need to know is that if you create anything inheriting from UnityEngine.Object at run time, you generally need to make sure you
    UnityEngine.Object.Destroy()
    it when no longer needed, cleaning up both the C# and C++ sides.

    Plain classes are just another tool you can use to code your project on the C# side of things. The way you use them is primarily other via traditional C# means, ergo, making and using instances on the fly, or serialising these instances into your monobehaviours of scriptable objects.

    Speaking of scriptable objects, this is the primary way you can outline classes so that they can be 'physical' assets used by other parts of your project.
     
    Ryiah, McPeppergames and DevDunk like this.
  3. McPeppergames

    McPeppergames

    Joined:
    Feb 15, 2019
    Posts:
    103
    Thank you for your fast feedback and help! Much appreciated!
     
  4. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,910
    • Class/Struct - Regular C# class/struct for encapsulating/grouping data/methods. Deleted by setting member variable to null. Local vars will auto-delete when out of scope (when function finishes).
    • MonoBehaviour - Class that can be added to GameObjects/Prefabs. Deleted by calling Destroy on it, or destroying parent GameObject.
    • ScriptableObject - Class where instances can exist as an asset on disk.
    • Tuple Types - Structure for data only. Deletion same as class/struct.

    When there are no references to a variable, it will become available for garbage collection.

    In Unity you will be mostly using Prefabs and adding MonoBehaviours (Components) to them by hand. Each component is an instance of that class. You access it after creating an instance of the Prefab, which becomes a GameObject in the scene. Example without any null checks:
    Code (csharp):
    1. public GameObject EnemyPrefab;
    2.  
    3. public void CreateThenDestroyEnemy()
    4. {
    5.     var enemyGameObject = Instantiate(EnemyPrefab); // Now enemy prefab exists in the scene as a GameObject
    6.  
    7.     var enemyHealthComponent = enemyGameObject.GetComponent<Health>(); // Access a Health MonoBehaviour/Component
    8.  
    9.     Debug.Log($"Enemy health is {enemyHealthComponent.CurrentHP}");
    10.  
    11.     Destroy(enemyHealthComponent); // Removes Health component
    12.     Destroy(enemyGameObject); // Destroys GameObject, including all components on it
    13. }
     
    Last edited: Oct 27, 2022
    McPeppergames and Bunny83 like this.
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    What do you mean by "physical" objects? Are talking about assets in the project? Instances in the scene? Something to do with the physics engine?
     
    Bunny83 likes this.
  6. McPeppergames

    McPeppergames

    Joined:
    Feb 15, 2019
    Posts:
    103
    Thank you very much! This helps a lot!!!
     
  7. McPeppergames

    McPeppergames

    Joined:
    Feb 15, 2019
    Posts:
    103
    I meant GameObjects, like a 3D model for a character in a scene... a prefab, etc.
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    What you're describing sound like assets, rather than GameObjects- unless you are talking about instantiating these at runtime or something. What I mean is- you are talking creating objects- do you mean how the game creates objects as it's running? I'm still struggling to figure out what you are asking, but it looks like some other people here have already figured it out. :D