Hello, Okay so I’m using this asset from the assets store 2D Tank Builder | Packs | Unity Asset Store but whenever I change the ‘Damage’ variable on one tank in changes the same variable on every tank with that script on it. It seems to do the same with all variables under the Weapon Controller script, but not on any of the others (for now). I did add some code to link it to one of my own scrips, however this is all I’ve changed;
`
switch (impactTarget.tag)
{
case “Mine”:
{
AssetManager.SpawnObject(“Mine Explosion”, impactTarget.position, impactTarget.rotation);
AudioManager.PlaySound(“Mine Explosion”);
Destroy(impactTarget.gameObject);
break;
}
case "EnemyTank":
{
Debug.Log("Hit!");
Tankdestruction tankdes = impactTarget.GetComponent<Tankdestruction>();
tankdes.Hit(Damage);
Explode();
break;
}
}
Explode();
}
`
I won’t share the entire code as it’s too long and I’m not sure if I should, but any help would be appreciate. It uses a lot of gets and sets and SerializeFields so I’m not sure if that could be something to do with it??
I shared that bit of code as that is the only part that I have changed, the whole code is far too long to share here. It also might be worth mentioning that the weaponcontroller creates an instance of a script called Weapon and then fires a bullet with an AmmoController script on it (three different scripts), the ‘Damage’ variable itself is on the Weapon script but the WeaponController script seems to handle the variables in the editor. I have tired searching literally everywhere but haven’t seen anything else come up like this. There are no statics that I can see however the Weapon script is a scriptable object. I’m sorry I’m still pretty new to coding and so I’m not really sure what is relevant in this context. I’ll share a few snippets of code which may be useful (The elipsis denotes code I have left out);
From the Weapon script:
using UnityEditor;
using UnityEngine;
namespace DimensionalDeveloper.TankBuilder.Utility
{
[CreateAssetMenu(fileName = "Weapon", menuName = "Tank Wars/New Weapon", order = 1)]
public class Weapon : ScriptableObject
{
#region Properties
#if UNITY_EDITOR
...
public float Damage
{
get => damage;
set => damage = Mathf.Max(0.0f, value);
}
[Tooltip("How much damage the ammo does.")]
[SerializeField] private float damage = 20.0f;
...
#if UNITY_EDITOR
public void OnValidate()
{
Name = _name;
ShotTimer = shotTimer;
Speed = speed;
Damage = damage;
ShotSound = shotSound;
ExplosionSound = explosionSound;
MuzzleFlash = muzzleFlash;
Explosion = explosion;
}
From the WeaponController script:
using DimensionalDeveloper.TankBuilder.Utility;
using DimensionalDeveloper.TankBuilder.Managers;
namespace DimensionalDeveloper.TankBuilder.Controllers
{
/// <summary>
/// The weapon controller, controls all aspects of the tanks weapons. Once a weapon scriptable object has been added,
/// it will manage all data within, use it and send the rest to the individual ammo controllers.
/// </summary>
public class WeaponController : MonoBehaviour
{
#region Properties
// The storage of components for easy access.
#region Cached Components
...
/// <summary>
/// Cached 'AssetManager' component.
/// </summary>
private new AssetManager AssetManager
{
get
{
if (_assetManager) return _assetManager;
_assetManager = AssetManager.Instance;
if(_assetManager == null) Debug.LogError("Ammo Controller: An asset manager was not found in the scene.");
return _assetManager;
}
}
private AssetManager _assetManager;
[HideInInspector] public bool[] hideSection = new bool[6];
// Storage of fire point transforms.
public List<Transform> firePoints = new List<Transform>();
// Three separate booleans for each cannon.
public string[] cannonInput = {"Main Shoot", "Left Shoot", "Right Shoot"};
public bool[] shootCannon = new bool[3];
public Weapon[] weapons = new Weapon[3];
public bool[] weaponDropdown = new bool[3];
...
}
}
Hope that makes sense as I kept hitting the word limit!
Are you changing it in the script or in the inspector? If it’s in the inspector, is it on a ScriptableObject? If that’s also the case then I assume that ScriptableObject is assigned to the components in the scene, meaning that they’re all reading from the same value. To assign a different value, create a new instance of the ScriptableObject and assign that.