Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How expensive are references to Scriptable Objects?

Discussion in 'Scripting' started by CodeFang, Apr 20, 2021.

  1. CodeFang

    CodeFang

    Joined:
    Nov 20, 2020
    Posts:
    12
    I have made a simple Inventory and Item system (with the help of a tutorial).

    It basically uses scriptable objects as Item blueprints and then creates an Item object from it. However it just passes the ID and name to the "Item" and if you want things like the sprite you look it up in some database(that has the "ItemScriptableObjects"). I understand that this is definitvly better than storing the Sprite in every actual item object performance-wise.

    However whenever I need to look something about the item I have to take a large retour over the database and the ID that results in pretty ugly code.

    I have been wondering if I couldn't just store the "ItemScriptableObject" in the "Item" once it is created. Would that result in a large performance hit? I assume it won't due to the fact that there will always be a single instance of each of these scriptable objects and I will just be creating a few references.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    The reference variable itself is 4 bytes. You're overthinking it.
     
    SparrowGS and CodeFang like this.
  3. CodeFang

    CodeFang

    Joined:
    Nov 20, 2020
    Posts:
    12
    good thats what I wanted to hear. I wonder why tutorial guy didn't do that. because otherwise his tutorial seemed pretty damn good and in-depth
     
    Last edited: Apr 20, 2021
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,921
    That sort of thing is large project loose coupling, Design Patterns and so on. Suppose you later decide not to store them as scriptableObjects. Maybe they won't exist at first and are created from JSON. Maybe they're stored on the central server. Using an ID number you can always look them up using a nice neutral getBluepritByID function. It's a trade-off: slower and more complex at first, but maybe better if your project lasts for 20 years with monthly revisions.and upkeep.
     
    CodeFang and StarManta like this.