I’m trying to change the player’s position when they kill a certain number of enemies. However, upon testing just one, the player doesn’t move. It isn’t the actual moving of the player, but an issue where the variable in a different class doesn’t change.
Player script:
public int enemiesDef;
void Update() // Called once per frame so will handle all of the inputs from the user
{
if (enemiesDef == 1)
{
player.transform.position = new Vector2(-54.3f, 0f);
enemiesDef = 0;
}
}
Enemy script:
public GameObject player;
private PlayerMovement pmove;
private void Start()
{
pmove = player.GetComponent<PlayerMovement>();
}
public void TakeDamage(float damageAmount) // Method that takes in a damage amount as a float and inflicts it upon the enemy
{
health -= damageAmount; // The damage amount is taken away from the current health
if (health <= 0) // If the enemy has not more health left once it has taken damage:
{
anim.SetTrigger("Death"); // Plays the death animation
Freeze(); // Calling the freeze method to stop the dead enemy from moving
StartCoroutine (Wait()); // Calling the Wait coroutine to wait a second and then remove the game object from the game
}
}
private IEnumerator Wait() // This is a coroutine to wait when an enemy dies
{
yield return new WaitForSeconds(1); // Waits for 1 second
Destroy(gameObject); // Then it dies
pmove.enemiesDef ++;
}