Search Unity

ScriptableObject allocates memory if the link is empty?

Discussion in 'Scripting' started by unity_5fpsmegasupergiperprogramer, Oct 3, 2019.

  1. unity_5fpsmegasupergiperprogramer

    unity_5fpsmegasupergiperprogramer

    Joined:
    Dec 1, 2017
    Posts:
    101
    Tell me how to do it in terms of memory optimization.

    I have a vehicle script. Only one vehicle has a weapon. I want to make a script of weapons. How best to keep a link to it? What would the vehicles without weapons have, was not the allocation of extra memory?

    As an option to make a script of weapons "ScriptableObject". And I add the variable "public VehicleWeapon weapon" to the script of the vehicle itself. If the field is empty, will the memory be allocated?
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    It sounds like you just need a private variable and a prefab variant.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    There's no memory cost difference between that field having a value or not. In either case it's just a pointer- either a pointer to the SO, or a pointer to null.

    A pointer's 8 bytes, so if you have 1000 of these vehicles, the additional memory cost of the public field is 8 kilobytes. Ie. you'll live.
     
  4. unity_5fpsmegasupergiperprogramer

    unity_5fpsmegasupergiperprogramer

    Joined:
    Dec 1, 2017
    Posts:
    101
    Thx!