- Home /
code in void not executing
can anyone help me plz? i've been trying to fix this problem for the past hour (i'm still fresh to c#) Basically i want to implement camera shake every time the enemy dies. I wrote the code for it in a public void alongside death effects and deletion of the gameobject. The weird thing is, when i kill the enemy through shooting, everything happens except the camera shake. i thought it was a code error but the weirder thing is that, when i kill the enemy through player-enemy collision (on contact death), the camera shake takes place. What's even weirder is that, btw I've written the enemy one first, I have the same exact one but for when the player dies, and it works. i'll write the code if anyone wants to check it
This one is for when the enemy dies
public void DamageEnemy(int damage)
{
stats.currentHealth -= damage;
if (stats.currentHealth <= 0)
{
GameMaster.KillEnemy(this);
Debug.Log("Enemy Dead");
}
if (statusIndicator != null)
{
statusIndicator.SetHealth(stats.currentHealth, stats.maxHealth);
}
}
This one is to do whatever I want when the enemy dies (everything works except the camera shake)
public static void KillEnemy(Enemy enemy)
{
gm._KillEnemy(enemy);
}
public void _KillEnemy(Enemy _enemy)
{
Destroy(_enemy.gameObject);
GameObject _clone = Instantiate(_enemy.deathParticles.gameObject, _enemy.transform.position, Quaternion.identity) as GameObject;
Destroy(_clone, 5f);
cameraShake.Shake(_enemy.shakeAmt, _enemy.shakeLength);
}
}
Answer by Rechronicle · Oct 10, 2021 at 02:53 PM
Destroy(_enemy.gameObject); // maybe because you destroy the enemy here?
GameObject _clone = Instantiate(_enemy.deathParticles.gameObject, _enemy.transform.position, Quaternion.identity) as GameObject;
Destroy(_clone, 5f);
cameraShake.Shake(_enemy.shakeAmt, _enemy.shakeLength); // then you try to call it here
If you use the exact same script for the player, it will work because you don't destroy the '_player' there.
But the thing is, the enemy gameobject destroys, the particles appear, they are then deleted (the 5f), but the camera shake doesn't happen. Plus, why you'd it only shake when the enemy dies to collision with player and not when it dies to being shot? i don't have any code that specifies it because whether the enemy dies to collision or gun, they are referred to that exact void.
Maybe try to put in debug right before the camera shake to see if the code runs or not? also, is there no console log error when you try to call it through shooting?
I implemented a debug before ("start of shaking") and after the camera shake ("end of shaking"). They both appear in the console but no shake happens. There are no errors as well.
Your answer
![](https://koobas.hobune.stream/wayback/20220613044023im_/https://answers.unity.com/themes/thub/images/avi.jpg)