Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to StartCoroutine using on click listerner

Discussion in 'Scripting' started by RCodelove, Sep 25, 2023.

  1. RCodelove

    RCodelove

    Joined:
    Aug 9, 2023
    Posts:
    22
    Code (CSharp):
    1. public void onPlayClicked()
    2.     {
    3.         frames = new List<string>() {
    4.             "Orient: x= 0.00 |       y= 0.00 |      z= 0",
    5.             "Orient: x= 10.00 |       y= 0.00 |      z= 0",
    6.             "Orient: x= 20.00 |       y= 0.00 |      z= 0",
    7.             "Orient: x= 30.00 |       y= 0.00 |      z= 0",
    8.             "Orient: x= 40.00 |       y= 0.00 |      z= 0",
    9.         };
    10.         // play = true;
    11.         StartCoroutine(PlayerCoroutine());
    12.     }


    I want to start coroutine and rotate gameobject using frame data, when I invocked onPlayClicked StartCoroutine(PlayerCoroutine()) doesn't work but when I put StartCoroutine(PlayerCoroutine()) in the Start() method gameObject rotate fine
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    I guess debug if
    onPlayClicked
    is being called at all with Debug.Log. If not... then there's your problem. May have forgotten to hook things up properly.

    Also why strings for this information? You could use a list of Vector3 values.
     
  3. RCodelove

    RCodelove

    Joined:
    Aug 9, 2023
    Posts:
    22
    I will use this string on loop and create vector3 values
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Why not just... make Vector3 values in the collection? It seems like an unnecessary step.
     
    RCodelove likes this.
  5. RCodelove

    RCodelove

    Joined:
    Aug 9, 2023
    Posts:
    22

    Kindly check this code


    Code (CSharp):
    1. List<string> frames = new List<string>() {
    2.             "Orient: x= 0.00 |       y= 0.00 |      z= 0",
    3.             "Orient: x= 10.00 |       y= 0.00 |      z= 0",
    4.             "Orient: x= 20.00 |       y= 0.00 |      z= 0",
    5.             "Orient: x= 30.00 |       y= 0.00 |      z= 0",
    6.             "Orient: x= 40.00 |       y= 0.00 |      z= 0",
    7.         };
    8.  
    9. IEnumerator PlayerCoroutine()
    10.     {
    11.         while (play && frameIndex < frames.Count)
    12.         {
    13.             frame = frames[frameIndex].Replace("Orient:", "");
    14.             frameIndex++;
    15.             yield return new WaitForSeconds(playSpeed);
    16.         }
    17.         play = false;
    18.     }
    19.  
    20. public void Update()
    21.     {
    22.         if (!play)
    23.         {
    24.             Debug.Log("******************************************");
    25.             StopCoroutine(PlayerCoroutine());
    26.         }
    27.         else if (frameIndex > frameIndexUpdated)
    28.         {
    29.             Debug.Log("==============================");
    30.             Debug.Log(frame);
    31.  
    32.             List<string> orient = frame.Split('|').Select(item => item.Trim()).ToList();
    33.  
    34.             string StringValue_OrientX = orient[0].Replace("x=", "").Trim();
    35.             string StringValue_OrientY = orient[1].Replace("y=", "").Trim();
    36.             string StringValue_OrientZ = orient[2].Replace("z=", "").Trim();
    37.  
    38.             float OrientX, OrientY, OrientZ;
    39.  
    40.             if (
    41.                 float.TryParse(StringValue_OrientX, out OrientX) &&
    42.                 float.TryParse(StringValue_OrientY, out OrientY) &&
    43.                 float.TryParse(StringValue_OrientZ, out OrientZ))
    44.             {
    45.                 Vector3 RotateAmount = new Vector3(OrientX, OrientY, OrientZ);
    46.                 Debug.Log(RotateAmount);
    47.                 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(RotateAmount), Time.deltaTime * 200f);
    48.             }
    49.         }
    50.     }
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Or you could just...

    Code (CSharp):
    1. List<Vector3> position = new List<Vector3>()
    2. {
    3.     new Vector3(0, 0, 0),
    4.     new Vector3(10, 0, 0),
    5.     new Vector3(20, 0, 0),
    6.     new Vector3(30, 0, 0),
    7.     new Vector3(40, 0, 0),
    8. };
     
  7. RCodelove

    RCodelove

    Joined:
    Aug 9, 2023
    Posts:
    22
    please help on button click because list can be seen later
     
  8. RCodelove

    RCodelove

    Joined:
    Aug 9, 2023
    Posts:
    22
    Code (CSharp):
    1. public void onPlayClicked()
    2.     {
    3.         Debug.Log("clicked clicked");
    4.         play = true;
    5.         StartCoroutine(PlayerCoroutine());
    6.     }
     
  9. RCodelove

    RCodelove

    Joined:
    Aug 9, 2023
    Posts:
    22
    Earlier it was working but now it is not working
     
  10. RCodelove

    RCodelove

    Joined:
    Aug 9, 2023
    Posts:
    22
    You can check the full code


    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using System.Globalization;
    6. using System.Linq;
    7. using TMPro;
    8.  
    9. public class BatRotate : MonoBehaviour
    10. {
    11.     List<string> frames = new List<string>() {
    12.         "Orient: x= -6.00 |       y= 1.00 |      z= 0",
    13.         "Orient: x= -7.00 |       y= 2.00 |      z= 0",
    14.         "Orient: x= -8.00 |       y= 3.00 |      z= 0",
    15.         "Orient: x= -9.00 |       y= 4.00 |      z= 0",
    16.         "Orient: x= -10.00 |       y= 5.00 |      z= 0",
    17.         "Orient: x= -11.00 |       y= 6.00 |      z= 0",
    18.         "Orient: x= -12.00 |       y= 7.00 |      z= 0",
    19.         "Orient: x= -13.00 |       y= 8.00 |      z= 0",
    20.         "Orient: x= -14.00 |       y= 9.00 |      z= 0",
    21.         "Orient: x= -15.00 |       y= 10.00 |      z= 0",
    22.         "Orient: x= -16.00 |       y= 11.00 |      z= 0",
    23.         "Orient: x= -17.00 |       y= 12.00 |      z= 0",
    24.         "Orient: x= -18.00 |       y= 13.00 |      z= 0",
    25.         "Orient: x= -19.00 |       y= 14.00 |      z= 0",
    26.         "Orient: x= -20.00 |       y= 15.00 |      z= 0",
    27.         "Orient: x= -21.00 |       y= 16.00 |      z= 0",
    28.         "Orient: x= -22.00 |       y= 17.00 |      z= 0",
    29.         "Orient: x= -23.00 |       y= 18.00 |      z= 0",
    30.         "Orient: x= -24.00 |       y= 19.00 |      z= 0",
    31.         "Orient: x= -25.00 |       y= 20.00 |      z= 0",
    32.         "Orient: x= -26.00 |       y= 21.00 |      z= 0",
    33.         "Orient: x= -27.00 |       y= 22.00 |      z= 0",
    34.         "Orient: x= -28.00 |       y= 23.00 |      z= 0",
    35.         "Orient: x= -29.00 |       y= 24.00 |      z= 0",
    36.         "Orient: x= -30.00 |       y= 25.00 |      z= 0",
    37.         "Orient: x= -31.00 |       y= 26.00 |      z= 0",
    38.         "Orient: x= -30.00 |       y= 25.00 |      z= 0",
    39.         "Orient: x= -29.00 |       y= 24.00 |      z= 0",
    40.         "Orient: x= -28.00 |       y= 23.00 |      z= 0",
    41.         "Orient: x= -27.00 |       y= 22.00 |      z= 0",
    42.         "Orient: x= -26.00 |       y= 21.00 |      z= 0",
    43.         "Orient: x= -25.00 |       y= 20.00 |      z= 0",
    44.         "Orient: x= -24.00 |       y= 19.00 |      z= 0",
    45.         "Orient: x= -23.00 |       y= 18.00 |      z= 0",
    46.         "Orient: x= -22.00 |       y= 17.00 |      z= 0",
    47.         "Orient: x= -21.00 |       y= 16.00 |      z= 0",
    48.         "Orient: x= -20.00 |       y= 15.00 |      z= 0",
    49.         "Orient: x= -19.00 |       y= 14.00 |      z= 0",
    50.         "Orient: x= -18.00 |       y= 13.00 |      z= 0",
    51.         "Orient: x= -17.00 |       y= 12.00 |      z= 0",
    52.         "Orient: x= -16.00 |       y= 11.00 |      z= 0",
    53.         "Orient: x= -15.00 |       y= 10.00 |      z= 0",
    54.         "Orient: x= -14.00 |       y= 9.00 |      z= 0",
    55.         "Orient: x= -13.00 |       y= 8.00 |      z= 0",
    56.         "Orient: x= -12.00 |       y= 7.00 |      z= 0",
    57.         "Orient: x= -11.00 |       y= 6.00 |      z= 0",
    58.         "Orient: x= -10.00 |       y= 5 |      z= 0",
    59.         "Orient: x= -9.00 |       y= 4 |      z= 0",
    60.         "Orient: x= -8.00 |       y= 3 |      z= 0",
    61.         "Orient: x= -7.00 |       y= 2 |      z= 0",
    62.         "Orient: x= -6.00 |       y= 1 |      z= 0",
    63.         "Orient: x= -5.00 |       y= 0 |      z= 0",
    64.         "Orient: x= -4.00 |       y= -1 |      z= 0",
    65.         "Orient: x= -3.00 |       y= -2 |      z= 0",
    66.         "Orient: x= -2.00 |       y= -3 |      z= 0",
    67.         "Orient: x= -1.00 |       y= -4 |      z= 0",
    68.         "Orient: x= 0.00 |       y= -5 |      z= 0",
    69.         "Orient: x= 1.00 |       y= -6 |      z= 0",
    70.         "Orient: x= 2.00 |       y= -7 |      z= 0",
    71.         "Orient: x= 3.00 |       y= -8 |      z= 0",
    72.         "Orient: x= 4.00 |       y= -9 |      z= 0",
    73.         "Orient: x= 5.00 |       y= -10 |      z= 0",
    74.         "Orient: x= 6.00 |       y= -11 |      z= 0",
    75.         "Orient: x= 7.00 |       y= -12 |      z= 0",
    76.         "Orient: x= 8.00 |       y= -13 |      z= 0",
    77.         "Orient: x= 9.00 |       y= -14 |      z= 0",
    78.         "Orient: x= 10.00 |       y= -15 |      z= 0",
    79.         "Orient: x= 11.00 |       y= -16 |      z= 0",
    80.         "Orient: x= 12.00 |       y= -17 |      z= 0",
    81.         "Orient: x= 13.00 |       y= -18 |      z= 0",
    82.         "Orient: x= 14.00 |       y= -19 |      z= 0",
    83.         "Orient: x= 15.00 |       y= -20 |      z= 0",
    84.         "Orient: x= 16.00 |       y= -21 |      z= 0",
    85.         "Orient: x= 17.00 |       y= -22 |      z= 0",
    86.         "Orient: x= 18.00 |       y= -23 |      z= 0",
    87.         "Orient: x= 19.00 |       y= -23 |      z= 0",
    88.         "Orient: x= 20.00 |       y= -24 |      z= 0",
    89.         "Orient: x= 21.00 |       y= -25 |      z= 0",
    90.         "Orient: x= 22.00 |       y= -26 |      z= 0",
    91.         "Orient: x= 23.00 |       y= -27 |      z= 0",
    92.         "Orient: x= 24.00 |       y= -28 |      z= 0",
    93.         "Orient: x= 25.00 |       y= -29 |      z= 0",
    94.         "Orient: x= 26.00 |       y= -30 |      z= 0",
    95.         "Orient: x= 27.00 |       y= -31 |      z= 0",
    96.         "Orient: x= 28.00 |       y= -32 |      z= 0",
    97.         "Orient: x= 29.00 |       y= -33 |      z= 0",
    98.         "Orient: x= 30.00 |       y= -34 |      z= 0"
    99.     };
    100.  
    101.     string frame = "";
    102.     int frameIndex = 0;
    103.     int frameIndexUpdated = 0;
    104.     private float playSpeed = 0.01f;
    105.     public TextMeshProUGUI speedTxt;
    106.     bool play = false;
    107.     IEnumerator myRoutine;
    108.  
    109.  
    110.     private void Start()
    111.     {
    112.         // play = true;
    113.         // StartCoroutine(PlayerCoroutine());
    114.     }
    115.  
    116.     IEnumerator PlayerCoroutine()
    117.     {
    118.         while (play && frameIndex < frames.Count)
    119.         {
    120.             frame = frames[frameIndex].Replace("Orient:", "");
    121.             frameIndex++;
    122.             yield return new WaitForSeconds(playSpeed);
    123.         }
    124.     }
    125.  
    126.     public void Play()
    127.     {
    128.         Debug.Log("clicked clicked");
    129.         play = true;
    130.         StartCoroutine(PlayerCoroutine());
    131.     }
    132.  
    133.     public void Stop()
    134.     {
    135.         play = false;
    136.         frame = "";
    137.         frameIndex = 0;
    138.         frameIndexUpdated = 0;
    139.         Vector3 dir = new Vector3(0, 0, 0);
    140.         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(dir), Time.deltaTime * 200f);
    141.         StopCoroutine(PlayerCoroutine());
    142.         return;
    143.     }
    144.  
    145.     void Update()
    146.     {
    147.         if (!play)
    148.         {
    149.             play = false;
    150.             frame = "";
    151.             frameIndex = 0;
    152.             frameIndexUpdated = 0;
    153.             Vector3 dir = new Vector3(0, 0, 0);
    154.             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(dir), Time.deltaTime * 200f);
    155.             StopCoroutine(PlayerCoroutine());
    156.         }
    157.  
    158.  
    159.         else if (frameIndex > frameIndexUpdated)
    160.         {
    161.             Debug.Log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    162.             List<string> orient = frame.Split('|').Select(item => item.Trim()).ToList();
    163.  
    164.  
    165.             string StringValue_OrientX = orient[0].Replace("x=", "").Trim();
    166.             string StringValue_OrientY = orient[1].Replace("y=", "").Trim();
    167.             string StringValue_OrientZ = orient[2].Replace("z=", "").Trim();
    168.             float OrientX, OrientY, OrientZ;
    169.  
    170.             if (
    171.                 float.TryParse(StringValue_OrientX, out OrientX) &&
    172.                 float.TryParse(StringValue_OrientY, out OrientY) &&
    173.                 float.TryParse(StringValue_OrientZ, out OrientZ))
    174.             {
    175.                 Vector3 dir = new Vector3(OrientX, OrientY, OrientZ);
    176.                 Debug.Log(dir);
    177.                 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(dir), Time.deltaTime * 200f);
    178.             }
    179.             else
    180.             {
    181.                 Debug.Log("Parsing failed for one or more values.");
    182.             }
    183.             frameIndexUpdated = frameIndex;
    184.         }
    185.  
    186.     }
    187.  
    188.  
    189.  
    190.  
    191.     // CLASS ENDS
    192. }