- Home /
C# Add game object to current list value
I am working on a weapon switching/pickup system for my FPS and am mostly finished. I currently have 3 weapons (the player can hold 2 at one time) but when I pick up a weapon it adds it to the list value of 1 even when the player is currently holding the weapon in position of 0, the gun picked up goes to list value 1 while the other existing gun goes down to (or stays at) list value 0. Is there any way to add the gun to the current list value? I have a public int in my code called 'arrayPos' that tracks which list value i am currently on (i change with the scroll wheel) and has the value stored if this helps? Any Help???
Thank YOU!
public int arrayPos = 0;
private void Update() {
if (arrayPos == 0 && weapons.Count > 0)
{
weapons[0].SetActive(true);
currentWeapon = weapons[0];
if (weapons.Count>1)
{
weapons[1].SetActive(false);
}
}
else if (arrayPos == 1 && weapons.Count > 0)
{
weapons[1].SetActive(true);
currentWeapon = weapons[1];
if (weapons.Count > 1)
{
weapons[0].SetActive(false);
}
}
//currentWeapon.SetActive(true);
if ((Input.GetAxis("Mouse ScrollWheel") > 0f) && weapons.Count > 1)
{
if (arrayPos >= 1)
{
arrayPos = 0;
}
else
{
arrayPos += 1;
}
}
if ((Input.GetAxis("Mouse ScrollWheel") < 0f) && weapons.Count > 1)
{
if (arrayPos <= 0 )
{
arrayPos = 1;
}
else
{
arrayPos -= 1;
}
}
if (Range_AK47 && Input.GetKeyDown("e") && weapons.Count < maxWeapons && weapons.Contains(AK47) == false)
{
weapons.Add(AK47);
}
if (Range_AK47 && Input.GetKeyDown("e") && weapons.Count <= maxWeapons && weapons.Contains(AK47) == false)
{
currentWeapon.SetActive(false);
weapons.Add(AK47);
weapons.Remove(currentWeapon);
}
if (Range_M4 && Input.GetKeyDown("e") && weapons.Count < maxWeapons && weapons.Contains(M4) == false)
{
weapons.Add(M4);
}
if (Range_M4 && Input.GetKeyDown("e") && weapons.Count <= maxWeapons && weapons.Contains(M4) == false)
{
currentWeapon.SetActive(false);
weapons.Add(M4);
weapons.Remove(currentWeapon);
}
if (Range_Pistol && Input.GetKeyDown("e") && weapons.Count < maxWeapons && weapons.Contains(Pistol) == false)
{
weapons.Add(Pistol);
}
if (Range_Pistol && Input.GetKeyDown("e") && weapons.Count <= maxWeapons && weapons.Contains(Pistol) == false)
{
currentWeapon.SetActive(false);
weapons.Add(Pistol);
weapons.Remove(currentWeapon);
}
}
Could you, please, share some code?
It's probably some logic problem.
Answer by RaphaelQuem · Sep 13, 2018 at 12:39 PM
There are some logic issues in that, for sure.
first, not correlated with your issue, this first if could be a lot simpler
if ( weapons.Count > 0)
{
weapons[arrayPos].SetActive(true);
currentWeapon = weapons[arrayPos];
if (weapons.Count > 1)
{
weapons[arrayPos].SetActive(false);
}
}
Now for the real problem, when you add a new weapon to the weapons list you do not change the arrayPos value.
I believe that you should always set it to the last index of your list, that is the index of your most recently picked up weapon. It's easier than trying to add the weapon to an index that is not the last. Something like this :
arrayPos = Mathf.Max(weapons.Count() - 1,0)
And as last comment, the if's you use to add weapons could be simpler and more readable as well, and i really think there is bug there. The only difference between the criterias is that the second one for each weapon has an equal sign in addition to the "minor than" that there was in the first one. So, if the first 'if' is true, the second one will also be. I would change it to be like this
if (Range_Pistol && Input.GetKeyDown("e") && weapons.Count < maxWeapons && !weapons.Contains(Pistol))
{
weapons.Add(Pistol);
}
else if (Range_Pistol && Input.GetKeyDown("e") && weapons.Count = maxWeapons && !weapons.Contains(Pistol))
{
currentWeapon.SetActive(false);
weapons.Add(Pistol);
weapons.Remove(currentWeapon);
}
// adding the logic to change arraypos here
Actually, i would just put and 'else', and put the range and count verification in a 'if' surrounding this one.
When i paste the ' arrayPos = $$anonymous$$athf.$$anonymous$$ax(weapons.Count() - 1,0)' line into void Start or update, it comes up with the error " The member `System.Collections.Generic.List.Count' cannot be used as method or delegate "
Yeah, my bad, just take the "()" off.
arrayPos = $$anonymous$$athf.$$anonymous$$ax(weapons.Count - 1,0)
I added this into void update (it made no difference in void start), however, i am not sure if it works because i am unable to scroll through my 'arrayPos' anymore XD. It just stays on the list value of 1. Thank you for the replies :)