Search Unity

Instantiate new instance of Scriptable Object in List

Discussion in 'Scripting' started by mrCharli3, Oct 19, 2019.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    I have a List of Scriptable Objects:

    Code (CSharp):
    1. public List<VillagerCooldown> cooldowns = new List<VillagerCooldown>();
    I have hundreds of villagers, and with this setup it seems the Scriptable object asset in the list is edited, which I do not want. I want to create a new instance of the SO, as if I did:

    Code (CSharp):
    1. public VillagerCooldown cooldown;
    2. cooldown = Instantiate(cooldown);
    The above code will not edit the actual SO.
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Assuming I understood you correctly, you want to do something like this?

    Code (CSharp):
    1. for( var i = 0; i < cooldowns.Count; i++ ) {
    2.   cooldowns[ i ] = Instantiate( cooldowns[ i ] );
    3. }