- 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, I wrote the enemy one first btw, I have the exact same set of code but for the player so there's a shake everytime the player dies, and it works!!! I'll write the code if anyone wants to check it
This one is to check if enemy is dead
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 carry out whatever I want it to do when the enemy is dead(everything works except the 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);
}
}
help would be appreciated
Try writing it as simple as possible:
Enemy.cs
public void ApplyDamage ( int damage )
{
stats.currentHealth -= damage;
if( stats.currentHealth<=0 )
{
GameObject fx = GameObject.Instantiate( deathParticles.gameObject , transform.position , Quaternion.identity );
Destroy( fx , 5f );
GameMaster.CameraShake( shakeAmt , shakeLength );
Destroy( _enemy.gameObject );
Debug.Log("Enemy Dead");
}
if( statusIndicator!=null )
{
statusIndicator.SetHealth( stats.currentHealth , stats.maxHealth );
}
}
GameMaster.cs
public static void CameraShake ( float amplitude , float duration )
{
gm.cameraShake.Shake( amplitude , duration );
Debug.Log($"{nameof(CameraShake)}( {nameof(amplitude)}:{amplitude} , {nameof(duration)}:{duration} )");
}
Your answer
Follow this Question
Related Questions
InvokeRepeating time doesn't sync within the same update function 2 Answers
Instantiated object not showing in scene or hierarchy 2 Answers
How to make the tower shoot at the one who is closer to the finish line? 0 Answers
How to use MySQL.Data dll in Unity 0 Answers
My balance in the game and my multiplier are not saving, i cant figure out why, any help? 0 Answers