how can i call a method only once from a fixed update function?

What I am trying to do is once the player dies he respawns to the beginning of the level. However, once the player respawns, it just keeps spawning over and over again and does not let me do anything. I am guessing that this because it is in the FixedUpdate() function. Is there any way to call the Die() function only once?

Here is my code:

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

	public float MovementSpeed;
	private Vector3 input;
	private float maxSpeed = 5f;
	private Vector3 spawn;
	public GameObject deathParticles;
	public GameManager manager;
	public AudioClip[] audioClip;
	public bool usesManager = true;

	private static bool Die = false;
	private bool checkpointEnabled = false;

	private Vector3 checkpoint;
	private Vector3 respawn;

	void Start () {
		spawn = transform.position;
		checkpoint = gameObject.transform.position;
		respawn = spawn;

		if (usesManager)
		{
			manager = manager.GetComponent<GameManager>();
		}

	}
	

	void FixedUpdate () {
		transform.Rotate(0,Input.GetAxis("Rotate")*60*Time.deltaTime,0);
		input = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical"));
		if(Die)
		{
			Instantiate(deathParticles, transform.position,Quaternion.Euler(0,0,0));
			transform.position = spawn;
		}
		if (checkpointEnabled)
		{
			respawn = checkpoint;
		}
		
		if (endLevel.isEndLevel)
		{
			respawn = spawn;
		}

		if(GetComponent<Rigidbody>().velocity.magnitude < maxSpeed)
		{
			GetComponent<Rigidbody>().AddRelativeForce(input * MovementSpeed);
		}

		if (transform.position.y < -2)
		{
			Die = true;
		}
		if (Die == true & checkpointEnabled == true)
		{
			transform.position = respawn;
		}
	
	}
	
	void OnTriggerEnter(Collider other)
	{
		if (other.gameObject.tag == "Checkpoint")
		{

			checkpointEnabled = true;
			Debug.Log("setCheckpoint: Player Hit Checkpoint.");
		}

		if (other.GetComponent<Collider>().tag == "Token")
		{

			if (usesManager)
			{
				manager.tokenCount +=1;
			}
			PlaySound(0);
			Destroy(other.gameObject);
		}
		if(other.GetComponent<Collider>().tag == "Enemy")
		{
			Die = true;
			PlaySound(2);
		}
		if(other.GetComponent<Collider>().tag == "Goal")
		{
			manager.CompleteLevel();
			Time.timeScale = 0f;
			PlaySound(1);
		}
	}

	void PlaySound(int clip)
	{
		GetComponent<AudioSource>().clip = audioClip[clip];
		GetComponent<AudioSource>().Play();
	}
}

Hi,

Whatever statement you use in any update methods will execute continuously.If you want a statement to execute only one inside any update methods,use a condition to execute it once.

I don’t see anywhere you reset the ‘Die’ boolean variable back to false in this script.Just set the Die boolean to false as soon as you spawned the death particles.

Like this…

void FixedUpdate()
{
    if(Die)
    {
        Instantiate(deathParticles,transform.position,Quaternion.Identity));
        transform.position = spawn;
        
        //State player is not dead any more
        Die=false;
    }
        
}

Or you can use another Boolean to execute the instantiate statement once.And reset the boolean to false wherever convenient.

bool PlayerSpawned = false;

void FixedUpdate()
{

  if(Die &&  !DeathParticleSpawned)
  {
    Instantiate(deathParticles,transform.position,Quaternion.Identity));
    transform.position = spawn;

    //State the death particles are spawned
    DeathParticleSpawned=true;
  }

}

Hope this helps :slight_smile: