When I call a function, It doesn't run completely
So this is a really weird occurrence for me, I've never had something like this happen. The function is being called from my weapon manager script, and it is to pick up the weapon. All it's doing is assigning it to some indexing array GameObject variables, calling the deploy function in the game object attached to the player, and then destroying the GameObject. The code works, however for some reason the first time I press the action key, it calls the Deploy() function (but doesn't run it fully) and also doesn't destroy the game object. The second time I press it, the game object is destroyed, and the rest of the deploy function is played.
Is this perhaps because the button call is only on the frame? I was to the understanding if a function is called, it is ran in its entirety. Code posted below:
Weapon Manager Script:
void Update()
{
if (Physics.Raycast(cam.position, cam.forward, out hit, distanceToPickup))
{
if (hit.collider.tag == "Weapon" && Input.GetButtonDown("Action"))
{
WeaponIndex index = hit.collider.GetComponent<WeaponIndex>();
if (currentGun != null)
{
currentGun.GetComponent<Firearm>().Deselect();
}
AvailableWeapons[index.weaponIndex] = WeaponsInGame[index.weaponIndex];
AvailableWeapons[index.weaponIndex].GetComponent<Firearm>().Deploy();
Destroy(hit.collider.gameObject);
} else if (hit.collider.tag == "Weapon")
{
Debug.Log("Press E to pickup");
} else {
Debug.Log("You're staring at nothing");
}
}
}
Weapon Script (Called "Firearm"):
public void Deploy()
{
gameObject.SetActive(true);
aSource.clip = deploy;
aSource.Play();
//play deploy animation
}
You are probably destroying the object before it has a chance to play its animation. Just move the destroy call to the deploy method. Ideally the Deploy method would be a coroutine, and you would yield for the length of the animation clip, then destroy the object. Also I think doing a GetComponent on a disabled object wont work, try this maybe :
AvailableWeapons[index.weaponIndex].SetActive(true);
StartCoroutine(AvailableWeapons[index.weaponIndex].GetComponent<Firearm>().Deploy());
public IEnumerator Deploy()
{
aSource.clip = deploy;
aSource.Play();
yield return new WaitForSeconds(deploy.Length);
Destroy(gameObject);
}
The object I'm trying to destroy is the scene model. The first time I press E the weapon appears in the player's hands, the second time I press it the scene model is destroyed, and the deploy audio is played. I've tried moving the destroy call up before (so it's destroyed before it's picked up so it's the illusion that it's the same gun) and I moved the play audio function up, and nothing happened.
I'm not trying to play an animation yet (I haven't made it yet, I just commented in there for my sake) however thanks for the coroutine comment. I was planning on changing my reload function to a coroutine as well.
Thanks for the reply!
I just tried commenting out the audio command, and it worked fine. Not sure why that would have anything to do with it.
Your answer
Follow this Question
Related Questions
[SOLVED]My code line don't execute 1 Answer
Running C# Function from JS file 0 Answers
is there a way to save data on Android before quit? 1 Answer
The laser problems 0 Answers
Add 2D Skeleton to the Scene if Button was pressed 0 Answers