- Home /
Problem with swithing weapons in a list of scriptable objects
First off Im like beginner/intermediate at programming and have A LOT to learn, so im sure this is a terrible way to do things.
I have a set of scriptable objects as weapons. A database script which holds a list of all the weapons. A Player scipt that holds a list of eqquiped weapons. and a playerShoot script which handles the switching weapons.
i was switching weapons with enums before which worked fine but it wouldnt allow for the player picking up new weapons and having those be assigned to 1, 2, or 3 on the keyboard dynamically (i.e first weapon picked up goes to number 1 slot). Thats why i switched to the list of equipped weapons, so i can add to the list upon pickup.
The problem is no matter what, the curWeapon variable is always set to the AsaultRifle scriptable object. i cant seem to change it through inputs. i can change it in the editor before running but its always asaultRifle during run time. There is nothing in update that is setting it every frame. its always that one scriptable object. is it not possible to change this type of variable?
ive included just the important parts of the scripts here because they are pretly clutter with notes and sectioned off pieces of code since im refactoring.
WeaponDatabase.cs
public class WeaponDatabase : MonoBehaviour
{
public List<WeaponScrObj> weapons;
}
Player.cs The list indexes are, 0 = Pistol, 1 = SMG, 2 = AsaultRifle
public class Player : MonoBehaviour
{
public WeaponDatabase weaponData;
public List<WeaponScrObj> equipedWeapons;
public WeaponScrObj curWeapon;
public int pistolAmmo;
public int smgAmmo;
public int rifleAmmo;
public int pistolClip;
public int smgClip;
public int rifleClip;
public float accuracy;
public float range;
public int damage;
public int clipSize;
public float fireRate;
public float reloadRate;
void Start()
{
weaponData = GetComponent<WeaponDatabase>();
equipedWeapons = new List<WeaponScrObj>();
equipedWeapons.Add(weaponData.weapons[0]);
equipedWeapons.Add(weaponData.weapons[1]);
equipedWeapons.Add(weaponData.weapons[2]);
curweapon = weaponData.weapons[0]
}
void Update()
{
accuracy = curWeapon.accuracy;
range = curWeapon.range;
damage = curWeapon.damage;
fireRate = curWeapon.fireRate;
clipSize = curWeapon.clipSize;
reloadRate = curWeapon.reloadRate;
if (curWeapon == weaponData.weapons[0])
{
accuracy = weaponData.weapons[0].accuracy;
range = weaponData.weapons[0].range;
damage = weaponData.weapons[0].damage;
fireRate = weaponData.weapons[0].fireRate;
clipSize = weaponData.weapons[0].clipSize;
reloadRate = weaponData.weapons[0].reloadRate;
weaponData.weapons[0].totalAmmo = pistolAmmo;
}
if (curWeapon == weaponData.weapons[1])
{
accuracy = weaponData.weapons[1].accuracy;
range = weaponData.weapons[1].range;
damage = weaponData.weapons[1].damage;
fireRate = weaponData.weapons[1].fireRate;
clipSize = weaponData.weapons[1].clipSize;
reloadRate = weaponData.weapons[1].reloadRate;
}
if (curWeapon = weaponData.weapons[2])
{
accuracy = weaponData.weapons[2].accuracy;
range = weaponData.weapons[2].range;
damage = weaponData.weapons[2].damage;
fireRate = weaponData.weapons[2].fireRate;
clipSize = weaponData.weapons[2].clipSize;
reloadRate = weaponData.weapons[2].reloadRate;
}
}
}
PlayerShoot.cs
if (Input.GetKeyDown(KeyCode.Alpha1))
{
player.curWeapon = player.equipedWeapons[0];
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
player.curWeapon = player.equipedWeapons[2];
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
player.curWeapon = player.equipedWeapons[3];
}
Answer by DivainUnity · Sep 13, 2021 at 06:54 PM
It seems like you do setting your curWeapon variable every frame on line 65
should be double ==if (curWeapon = weaponData.weapons[2])
Also, inside your PlayerShoot.cs, check your numbering, seems like you're out of scope there (0, 2 ,3 seems like it should be (0, 1, 2).
Good lord, its always the little things. That did the trick. Thanks so much for being my second eyes here. I wish there was an error or note when you use a single = in an if condition, like "Thats not a question dummy...". Feeling a little sheepish about stating nothing in the update is changing in bold.
Ironically i just answered someones question that had the same issue. just a small symbol was wrong. Thanks Again!!
It happens to the best of us so I'm glad to be your second eyes! Please don't forget to vote this answer up
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Trying to use lists to check for available spawn points 2 Answers
InvokeRepeating help 2 Answers