Scriptable object, same object with individual values

39 Views Asked by At

I have tried every method. For example i created a BasicGun from this attributes. My character can equip more than 1 from this gun. But values are not acting individually. For example i have a BasicGun and started to attack players and my damagedealt increased to 112, when i equip a new gun its damagedealt 112 too. If the object is same values not acting individually .

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

[CreateAssetMenu(fileName = "NewGun", menuName = "Weapons/Attributes")]
public class WeaponAttributes : ScriptableObject
{
    public string weaponName;
    public int defaultDamage;
    public float defaultAttackSpeed;
    public float defaultLifeSteal;
    public float defaultAttackRange;

    public Sprite defaultWeaponSprite;
    public int defaultDamageDealt;

    // Current values
    public int damage;
    public float attackSpeed;
    public float lifeSteal;
    public float attackRange;
    public GameObject bulletPrefab;
    public GameObject weaponPrefab;
    public Sprite weaponSprite;
    public int damageDealt;

    // Reset method
    public void ResetToDefaults()
    {
        damage = defaultDamage;
        attackSpeed = defaultAttackSpeed;
        lifeSteal = defaultLifeSteal;
        attackRange = defaultAttackRange;

        weaponSprite = defaultWeaponSprite;
        damageDealt = defaultDamageDealt;
    }

    public void Initialize()
    {

        defaultDamage = damage;
        defaultAttackSpeed = attackSpeed;
        defaultLifeSteal = lifeSteal;
        defaultAttackRange = attackRange;

        defaultWeaponSprite = weaponSprite;
        defaultDamageDealt = damageDealt;
    }



    public WeaponAttributes Clone()
    {
        WeaponAttributes newAttributes = CreateInstance<WeaponAttributes>();
        newAttributes.weaponName = weaponName;
        newAttributes.defaultDamage = defaultDamage;
        newAttributes.defaultAttackSpeed = defaultAttackSpeed;
        newAttributes.defaultLifeSteal = defaultLifeSteal;
        newAttributes.defaultAttackRange = defaultAttackRange;
   
        newAttributes.defaultWeaponSprite = defaultWeaponSprite;
        newAttributes.defaultDamageDealt = defaultDamageDealt;

        // Initialize current values with defaults

        return newAttributes;
    }
}
2

There are 2 best solutions below

0
robotrage On

You are setting your damage on init to the object's value? instead of using "default damage" and "damage" and setting the damage from the default, just have one variable "damage" on your prefab and set the default public value on the prefab in the inspector. when that prefab gets instantiated it should have the default value assigned.

1
demonjoseph On

i realized that there is no reason to initialize or reseting attributes, i just passed the wrong cloned object to the scenes and scripts, now its works fine even no need for default attributes as you guys says, because im always using clone objects so base object's values not changing