Search Unity

How to use Coroutine on IDropHandler? If impossible, any alternatives?

Discussion in 'Scripting' started by vaporaworker, Jun 23, 2020.

  1. vaporaworker

    vaporaworker

    Joined:
    Jun 18, 2020
    Posts:
    1
    Hi Guys!

    Ok I've been trying to use Coroutines on the IDropHandler(for a drag and drop feature) so that the Tween Animations on my game to play but it seems that IDropHandler only calls it once and the Coroutines yield return new WaitForSeconds doesn't seem to end and move on to the Method.

    // scr_drop script is attached to a single object that manages what is drop on top of it
    Code (CSharp):
    1. public class scr_drop : MonoBehaviour, IDropHandler
    2. {
    3.  
    4.    [SerializeField]scr_blue_screen ref_Blue_screen; // blue screen gameobject
    5.    [SerializeField]scr_set_skate_default_location ref_set_skte_def_loc;
    6.    [SerializeField]scr_card_manager ref_card_manager;
    7.    [SerializeField]scr_audio_manager ref_audio_manager;
    8.    [SerializeField]scr_card_win_restart_function ref_win_restart;
    9.    [SerializeField]int the_correct_answer; // a value meant for the answer/angle card array to cycle thru them
    10.    [SerializeField]int card_index; // a value meant for the skateboard card arrays to cycle thru
    11.  
    12.  
    13.     public void OnDrop(PointerEventData eventData)
    14.     {
    15.  
    16.         // Resets the Angle_Cards to its default canvas group state
    17.         eventData.pointerDrag.GetComponent<CanvasGroup>().alpha = 1f;
    18.         eventData.pointerDrag.GetComponent<CanvasGroup>().blocksRaycasts = true;
    19.  
    20.         // Set evenData.pointerDrag(Answer Card) to its default location after dropping on the Skateboard
    21.         eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition =
    22.         ref_set_skte_def_loc.GetComponent<scr_set_skate_default_location().
    23.         Prop_Default_Angle_Choice_Location[the_correct_answer];
    24.  
    25.         // Checks if evenData.pointerDrag matches the ref_Angle_choice_card with the correct answer
    26.         if(eventData.pointerDrag == ref_card_manager.Prop_Angle_Choice_Cards[the_correct_answer])
    27.         {  
    28.             // Set the the skate_card_index and answer_cor_card to its value
    29.             ref_card_manager.Prop_Array_Cards[card_index].GetComponent<scr_tween_skate().
    30.             skate_card_index = card_index;
    31.             ref_card_manager.Prop_Array_Cards[card_index].GetComponent<scr_tween_skate().
    32.             answer_cor_card = the_correct_answer;
    33.             // Warning Sign Off
    34.             if(ref_win_restart.warning_sign.activeSelf == true)
    35.             {
    36.                 ref_win_restart.warning_sign.SetActive(false);
    37.             }
    38.             // Start the Coroutine for the delay correct card
    39.             StartCoroutine(ref_card_manager.Prop_Array_Cards[card_index].
    40.             GetComponent<scr_tween_skate().Delay_Correct_Card());
    41.             // Makes the Skateboard button off
    42.             ref_card_manager.Prop_Array_Cards[card_index].GetComponent<Button>().enabled = false;
    43.             // Set Blue Screen off
    44.             ref_Blue_screen.Prop_Blue_Screen_Object.SetActive(false);
    45.         }

    // scr_tween_skate // this script is attached to individual gameobjects
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class scr_tween_skate : MonoBehaviour
    7. {
    8.     [SerializeField]scr_card_manager ref_card_manager;
    9.     [SerializeField]scr_audio_manager ref_audio_manager;
    10.     [SerializeField]GameObject skate_card;
    11.     [SerializeField]float RotationZ;
    12.     [SerializeField]float duration;
    13.     [SerializeField]float delay;
    14.     public int skate_card_index;
    15.     public int answer_cor_card;
    16.     public bool is_apply_delayed_anim = false;
    17.     float startTime;
    18.     void Start()
    19.     {
    20.         skate_card = this.gameObject;
    21.     }
    22.     public IEnumerator Delay_Correct_Card()
    23.     {          
    24.         // Swap Skateboard Cards with one of the 4 Answer Cards
    25.         yield return new WaitForSeconds(delay);
    26.         Debug.Log("Delay_Correct_Card");
    27.         ref_card_manager.Prop_Array_Cards[skate_card_index].GetComponent<Image>().sprite =
    28.         ref_card_manager.Prop_Angle_Choice_Cards[answer_cor_card].GetComponent<Image>().sprite;
    29.  
    30.         //Play Correct Audio
    31.         ref_audio_manager.Play_Audio("after_ans_appear_dog_page");
    32.         //Start Delay Rotate Tween
    33.         StartCoroutine(Delay_Rotate_Card());        
    34.     }
    35.  
    36.     public IEnumerator Delay_Rotate_Card()
    37.     {   Debug.Log("Delay_Rotate_Card");
    38.         yield return new WaitForSeconds(delay);
    39.         Rotate_Tween_Z();
    40.     }
    41.  
    42.     public void Rotate_Tween_Z()
    43.     {
    44.         LeanTween.rotateZ(skate_card,RotationZ,duration).setOnComplete(Disable);
    45.         ref_audio_manager.Play_Audio("crct_card_spin_aud");
    46.     }
    47.  
    48.     void Disable()
    49.     {
    50.         skate_card.SetActive(false);
    51.         ref_audio_manager.Play_Audio("crct_after_card_spin_aud");
    52.         // Increment the num of answered correctly to fulfil win condition
    53.         GameObject.FindObjectOfType<scr_card_win_restart_function>().Prop_Num_Answered_Correctly++;
    54.         is_apply_delayed_anim = false;
    55.         delay = 2.0f;
    56.     }
    57. }
    58.