Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Stoyanow · Jul 09, 2015 at 11:08 AM · coroutinesienumerator

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 ^^

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

Answer by Stoyanow · Jul 10, 2015 at 09:40 AM

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;
         
     }
 
     
 }







Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by LaneFox · Jul 09, 2015 at 11:27 AM

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();

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Run Coroutine once, but finish Lerping 2 Answers

Coroutine couldn't be started because the the game object 'Attack' is inactive! 1 Answer

Coroutines not passing yield 1 Answer

Having trouble using coroutines, making unity hang 0 Answers

Can I use StartCoroutine(CoroutineName()) instead of StartCoroutine("CoroutineName") from another script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges