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

Changing a Scriptable Object's Variables without effecting other instances.

Discussion in 'Scripting' started by joesrelo, Jul 4, 2020.

  1. joesrelo

    joesrelo

    Joined:
    Mar 30, 2020
    Posts:
    4
    Hi, second time posting. I hope this is the correct category for this question.

    So I'm making a Looter Shooter, similar to Destiny or Borderlands. However, I'm having an Issue with random rolls on weapons.

    Let me explain:

    Let's say you get a drop from an enemy, it will give you a random weapon type from its lootpool. The part that is an issue is when I attempt to add random stats to a weapon it applies those changes to all weapons of that type.

    I.E. You get an AR with the perk Swiftness, and it changes the perk set on all the ARs previously accquired to have that stat.

    The original implementation before I realized this issue was this:

    if (item.hasRandomRolls)
    {
    item.activePerk1 = item.perkSlot1[UnityEngine.Random.Range(0, item.perkSlot1.Count - 1)];
    if (item.hasTwoPerks)
    {
    item.activePerk2 = item.perkSlot1[UnityEngine.Random.Range(0, item.perkSlot2.Count - 1)];
    }
    }

    I tried to fix it by creating a new item of the Gun scriptable object, adding a middle-man:

    Gun item = new Gun();
    item = pickup
    if (item.hasRandomRolls)
    {
    item.activePerk1 = item.perkSlot1[UnityEngine.Random.Range(0, item.perkSlot1.Count - 1)];
    if (item.hasTwoPerks)
    {
    item.activePerk2 = item.perkSlot1[UnityEngine.Random.Range(0, item.perkSlot2.Count - 1)];
    }
    }

    However, both resulted in the same conclusion: the change in all the previously acquired weapons as the newest stat roll is acquired.

    My hypothesis:
    When I go and apply the perks, it applies it to the scriptable object class that it derives from. My second attempt was created as a way to make another scriptable object instance in order to receive the new stats without changing the currently acquired weapons. However, since object = statements are just adjusting references to memory, it is effected by the change as well.

    The ideal scenario:
    I can just create a new instance of the Gun scriptable object, keeping the base characteristics of the weapon itself while changing the active perk variable in order without affecting other weapons of the same type in inventory.

    If there is a usable solution it would be greatly appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
  3. joesrelo

    joesrelo

    Joined:
    Mar 30, 2020
    Posts:
    4
    Kurt-Dekker likes this.