Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem with instance of a ScriptableObject (class)

Discussion in 'Scripting' started by Bakudan, Feb 25, 2020.

  1. Bakudan

    Bakudan

    Joined:
    Mar 29, 2015
    Posts:
    16
    Hi there, so i got a scriptable object with some datas.

    Code (CSharp):
    1.        
    2. actors = new List<Character_Class_Full>();
    3.  
    4. Character_Class_Full[] allCharas = Resources.LoadAll<Character_Class_Full>("ScriptableObjects/Characters_Templates");
    5.         Character_Class_Full[] allEnemies = Resources.LoadAll<Character_Class_Full>("ScriptableObjects/Enemies_Templates");
    6.         for(int i = 0; i < playersNumber; i++){
    7.             actors.Add(allCharas[i+1]);
    8.         }
    9.         for (int i = 0; i < enemiesNumber; i++)
    10.         {
    11.             Character_Class_Full newEnemy = ScriptableObject.CreateInstance<Character_Class_Full>();
    12.             newEnemy = allEnemies[0];
    13.             actors.Add(newEnemy);
    14.         }
    So let say i got actor[4] actor[5] actor[6] feed with the Scriptable Object in allEnemies[0]

    The problem is if i try to modify actor[4].life = 50, is also set my actor[5] actor[6].life to the same values.
    I don't understand why those 3 instantiated classes are linked together (i'm new to coding). Am i not setting the newEnemy = allEnemies[0]; correctly ?

    Thanks in advance.