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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Cloning Scriptable Items

Discussion in 'Scripting' started by Loff, Apr 11, 2019.

  1. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    I have tried a lot of different ways, but haven't found one that works so here I am.

    Current code works, but it uses the original scriptable Object instead of creating a clone. The clones needs to have the same vars and values as the Originals


    Code (CSharp):
    1.  static void LoadItems()
    2.     {
    3.             Object[] Items = Resources.LoadAll("ScriptableObjects/StoreObjects/", typeof(Item));
    4.  
    5.             foreach (Item i in Items)
    6.             {
    7.                 Item item = ScriptableObject.Instantiate(i);
    8.                 ItemDatabase.Add(item);
    9.             }
    10.             Debug.Log("Items in database: " + ItemDatabase.Count);
    11.     }
    What it does
    1. Load all items from Resource folder
    2. Adds the orginal Scriptable Object into the database
    What I want the code to do:
    1. Load all Scriptable Objects from Resource folder
    2. Make a copy of the Scriptable Objects and add them to the ItemDatabase List<Item>.
    What makes this hard:
    1. Item is a parent class.
    2. it has child classes like: Harddisk, Server..
    3. The Script that holds the itemdatabase is a public static.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You are making a clone when you call Instantiate though. Also, all ScriptableObjects are "copies" in builds so are you certain you actually need to copy them in the first place? It should be fine as long as your data is immutable.
     
  3. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    if I look at the original Scriptable Object in resource folder, it changes the values when I change the clone's values ingame. Pretty sure that wasn't the case in earlier project when I managed to create a real clone without static and all that.
    Not sure what Immutable means, but if I start my project for example and do changes to the scriptable object in play mode, then stop playmode the values will stay have saved the changes automatically.

    Follow up:
    I tried
    Code (CSharp):
    1.  public void AddTestObjects()
    2.     {
    3.         foreach (Item item in GlobalVars.ItemDatabase)
    4.         {
    5.             Inventory.Items.Add(Instantiate(item));
    6.         }
    7.     }
    still the same result. Doing a edit to scriptable object ingame also changes value to original one.

    *Facepalm*
    erhm.. I figured it out - all scripts above works as it should - the mistake is that one of objects I use had the SO directly in the inspector, and that is why the values was changed after play testing.. :)
     
    Last edited: Apr 11, 2019
    GroZZleR likes this.