IEnumerator OnTriggerEnter2D IF statement

Hello great people. I created a script, to warp the player to a game object’s position, and fade in/fade out when doing so. I want to create an IF statement, which requires the player to press space in order to warp. I tried putting it in update, fixed update, tried yield return new WaitForFixedUpdate, but no success so I am asking here. Here is the script(it’s a bit of a mess I am sorry):

using UnityEngine;
using System.Collections;

public class Warp : MonoBehaviour {

	public Transform warpTarget;

	public Player player;

	private bool playerInZone;


	public void FixedUpdate(){

		if(Input.GetKeyDown(KeyCode.Space) && playerInZone)
			WarpNow();
	}



	IEnumerator OnTriggerEnter2D(Collider2D other){
		 
		if(other.name == ("Player")){
			playerInZone = true;
		}

		ScreenFader sf = GameObject.FindGameObjectWithTag("Fader").GetComponent<ScreenFader>();


		yield return StartCoroutine (sf.FadeToBlack ());

		WarpNow();

		yield return StartCoroutine (sf.FadeToClear ());
		}

	void OnTriggerExit2D(Collider2D other){

		playerInZone = false;
	}

	public void WarpNow(){

	
		player.gameObject.transform.position = warpTarget.transform.position;

	}

}

I really hope someone can help me ^^

I don’t think you can make OnTriggerEnter() a Coroutine.

You probably need to keep it as a void and start the coroutines from there instead. You should also cache the fader instead of finding it every time there is a collision.

        void Start()
        {
            sf = GameObject.FindGameObjectWithTag("Fader").GetComponent<ScreenFader>();
        }
        void OnTriggerEnter(Collider other)
        {
            StartCoroutine(HitSomething());
        }

        private IEnumerator HitSomething()
        {
            yield return StartCoroutine(sf.FadeToBlack());
            WarpNow();
            yield return StartCoroutine(sf.FadeToClear());
        }

I also haven’t tested yielding to a coroutine in another script, but i guess it should work.

Ideally you should just do this on the fader, like in OnTriggerEnter() just do sf.StartCoroutine.HitSomething();

Fixed it. Here is the script in case someone stumbles on this in the future.

using UnityEngine;
using System.Collections;

public class Warp2 : MonoBehaviour {
	
	public Transform warpTarget;
	
	public Player player;
	
	private bool playerInZone;


	
	void Update(){
		
		if(Input.GetKeyDown(KeyCode.Space) && playerInZone )



			StartCoroutine(Fading());
			
			

	}

	
	void OnTriggerEnter2D(Collider2D other){

		if(other.name == "Player")
			playerInZone = true;
	}

	void OnTriggerExit2D(Collider2D other){
		
		playerInZone = false;
	}

	IEnumerator Fading(){
	

		player.GetComponent<Animator>().enabled = false;

		player.GetComponent<Player>().enabled = false;

		player.maxSpeed = 0f;

		ScreenFader sf = GameObject.FindGameObjectWithTag("Fader").GetComponent<ScreenFader>();
		
		
		yield return StartCoroutine (sf.FadeToBlack ());
		
	
		WarpNow();

		
		yield return StartCoroutine (sf.FadeToClear ());


		player.GetComponent<Animator>().enabled = true;

		player.GetComponent<Player>().enabled = true;

		player.maxSpeed = 0.5f;
	}
	

	
	public void WarpNow(){
		


		player.gameObject.transform.position = warpTarget.transform.position;
		
	}

	
}