- Home /
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;
}
Answer by WarmedxMints · Feb 28, 2019 at 04:23 PM
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.
Your answer
Follow this Question
Related Questions
fully automatic gun script not working 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Bullet Penetration 1 Answer
Creating laser gun effect. 1 Answer