Setting gameobject to null

Hey guys,

So im trying to make a weapon system where the player gun pick up a weapon, the idea that i have of this is as follows. The player has a GameObject called curGun. When the player interacts with another weapon through the IInteractable interface the following code runs and it is supposed to set the players curGun to null. But this is obviously not working. Anyways after the coroutine finishes, the players curGun is supposed to set be the same as the GameObject thisWeapon. If you guys know how to fix this, or of a better way to do this, id appreciate the help.

    IEnumerator PickingUpGun()
    {
        playerC.curGun = null;
        akm.SetActive(false);
        yield return new WaitForSeconds(5);
        akm.SetActive(true);
    }

    public void Interact()
    {
        print("You are unarmed");
        StartCoroutine(PickingUpGun());
        playerC.curGun = thisWeapon;
    }

Move the line playerC.curGun = thisWeapon to the end of the coroutine if you don’t want it assigned until it finishes. Either that or remove the line setting it to null.

The method Interact continues running after it starts the Coroutine, it doesn’t wait for it to finish. That’s one of the advantages of coroutines, you can execute code over multiple frames.