Hello,
I am relatively new to Unity and am currently going crazy because of a problem.
in a 2D topdown game I try to swap my current weapon with the weapon lying on the ground. Both have the “WeaponSystem” script with the following code for pickup/drop:
private void Update()
{
MyInput();
}
private void MyInput()
{
if(Input.GetKeyDown(KeyCode.E) && findPlayerInRadius.inRange && !isActive)
{
GameObject activeWeapon = pivotpoint.transform.GetChild(0).gameObject;
activeWeapon.GetComponent<GunSystem>().Drop();
PickUp();
}
}
private void PickUp()
{
isActive = true;
//Attach Weapon to Player
transform.parent = pivotpoint.transform;
transform.localPosition = pos;
transform.localRotation = Quaternion.identity;
transform.localScale = new Vector3(1, 1, 1);
}
private void Drop()
{
isActive = false;
//Unattach Weapon to Player
transform.parent = null;
transform.localRotation = Quaternion.identity;
transform.localScale = new Vector3(1, 1, 1);
}
According to the Visual Studio Debugger, the process now looks as follows: The MyInput() method is called for my object lying on the ground which has the effect that the active weapon is dropped and is therefore no longer “active”. The weapon lying on the ground is then picked up and set “Active”.
So far so good.
But then the problem occurs: the MyInput method is called again for the originally active weapon which is now lying on the ground. This has the effect of picking up the weapon and dropping the other one. In the game it looks like I don’t pick up the other weapon at all, because it is dropped again immediately.
Unfortunately, I don’t know how to get ahead at this point.
I hope you can help me^^