Search Unity

Question What's the most efficient way to access other classes methods?

Discussion in 'Scripting' started by Flawkee, Apr 10, 2021.

  1. Flawkee

    Flawkee

    Joined:
    Sep 24, 2020
    Posts:
    2
    Hi,
    I'm learning Unity with GameDev.TV courses and I didn't have much knowledge with programming, just the very basics.

    Now I'm starting to think what is the most efficient way to access methods from other classes? (Objects that are instantiated into the scene).
    Now let's say it is a class with 1 instance only, which means there's only a single game object with the relevant script attached as a component.
    In order to do so, I must have a reference to an instance of the class (to the game object) and as far as I know these are the options available:

    1. Using [SerializedField] PlayerStats, this will basically allow me to use the Unity Editor and attach the relevant game object and will enable me to access it's class and public methods.

    2. Using FindObjectsOfType<PlayerStats>() - It will allow me to search all the objects in the scene for the one I'm actually looking for and will return the first game object that match. However, I heard that this option is the most inefficient way to do so especially in a scene with a lot of game objects and constant use of this method.

    3. Using static instance - I can basically create a static reference like:

    Code (CSharp):
    1. public class PlayerStats : MonoBehaviour
    2. {
    3.        public static PlayerStats instance;
    4. }
    and then I can access all methods / vars through PlayerStats.instance.

    In case I need an object / variables across different scenes than I can use:
    A. static vars
    B. singleton pattern

    Now as I said, I'm a beginner and I learn from the course and digging into google, but some explanations I have found I can barely understand since I'm not a real programmer, not yet...
    So is there any right? any wrong?
    Other ways I am not aware of?
    Which one is the more efficient way?

    Thank you!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    You can kinda structure it however you like. That's a bit daunting but the main considerations are: a) what you feel comfy working with, and b) what fits the lifetime of the things in your game.

    Personally I hate having to drop anything into a scene, since it is so easy to forget, and makes things awkward and annoying.

    For anything that lives longer than a scene, this is what I like to use, either the data-less or the data-full (prefab or ScriptableObject) versions:

    Some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data):

    https://pastebin.com/SuvBWCpJ

    Unity3D Singleton with Prefab used for predefined data:

    https://pastebin.com/cv1vtS6G

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

    If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

    Code (csharp):
    1. public void DestroyThyself()
    2. {
    3.    Destroy(gameObject);
    4.    Instance = null;    // because destroy doesn't happen until end of frame
    5. }