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 Coroutine is not invoking on click listener funtion(Urgent!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1)

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

  1. RCodelove

    RCodelove

    Joined:
    Aug 9, 2023
    Posts:
    22
    The game object is rotating fine when StartCoroutine(PlayerCoroutine()) is used in Start but when I called it from Play() the game object doesn't rotate



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Linq;
    5.  
    6. public class BatRotate : MonoBehaviour
    7. {
    8.     bool play = true;
    9.     List<string> frames = new List<string>() {
    10.         "Orient: x= 0.00 |       y= 0.00 |      z= 0.00",
    11.         "Orient: x= 90.00 |       y= 0.00 |      z= 0.00"
    12.     };
    13.     string frame = "";
    14.     int frameIndex = 0;
    15.     int frameIndexUpdated = 0;
    16.  
    17.     private void Start()
    18.     {
    19.     }
    20.  
    21.     IEnumerator PlayerCoroutine()
    22.     {
    23.         while (play && frameIndex < frames.Count)
    24.         {
    25.             frame = frames[frameIndex].Replace("Orient:", "");
    26.             frameIndex++;
    27.             yield return new WaitForSeconds(0.5f);
    28.         }
    29.         play = false;
    30.     }
    31.  
    32.     public void Play()
    33.     {
    34.         StartCoroutine(PlayerCoroutine());
    35.     }
    36.  
    37.     void Update()
    38.     {
    39.         if (!play)
    40.         {
    41.             StopCoroutine(PlayerCoroutine());
    42.         }
    43.         else if (frameIndex > frameIndexUpdated)
    44.         {
    45.             List<string> orient = frame.Split('|').Select(item => item.Trim()).ToList();
    46.             string StringValue_OrientX = orient[0].Replace("x=", "").Trim();
    47.             string StringValue_OrientY = orient[1].Replace("y=", "").Trim();
    48.             string StringValue_OrientZ = orient[2].Replace("z=", "").Trim();
    49.  
    50.             float OrientX, OrientY, OrientZ;
    51.  
    52.             if (float.TryParse(StringValue_OrientX, out OrientX) &&
    53.                 float.TryParse(StringValue_OrientY, out OrientY) &&
    54.                 float.TryParse(StringValue_OrientZ, out OrientZ))
    55.             {
    56.                 Vector3 dir = new Vector3(OrientX, OrientY, OrientZ);
    57.                 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(dir), Time.deltaTime * 200f);
    58.             }
    59.             else
    60.             {
    61.                 Debug.Log("Parsing failed for one or more values.");
    62.             }
    63.         }
    64.     }
    65. }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    This just looks like a duplicate of your existing thread here. Why is it urgent?

    Looking at your thread, it's starting to look like you're not debugging this yourself but instead are desperately asking others to do so for you; something that cannot easily be done.

    I don't even see you calling the "Play" method above so I'm not sure what the code above is trying to show beyond what you posted in your previous thread.
     
    Yoreki likes this.
  3. RCodelove

    RCodelove

    Joined:
    Aug 9, 2023
    Posts:
    22
    Actually I am debugging a lot and I am new to unity, this was working alright I don't what happend now it's not working,
    basically I want to rotate the gameobject with the frame data, it's is working on Start method but Play() method it is not working, I am stuck with for many days, that's why I added urgent bro
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    I can appreciate it's hard when you're new to Unity but also, you're not helping yourself by not replying to things being asked of you such as why use strings for this (other thread) and that you're not calling the Play method above. It's hard to help if you don't interact correctly.

    So.....

    Call the method above in your head then figure out what the coroutine would do. Where do you set the "play" field to true? If this is not true, the coroutine will clearly do nothing and you cancel the coroutine if this is the case. This is what debugging is. Better still, attach a debugger and step through it yourself. Have you done this?

    Here's a guide that might help: https://docs.unity3d.com/Manual/ManagedCodeDebugging.html
     
    Yoreki and RCodelove like this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Are you? It feels like you may not be doing it correctly. Try this approach:

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    If your problem is with OnCollision-type functions, print the name of what is passed in!

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
    Yoreki likes this.
  6. RCodelove

    RCodelove

    Joined:
    Aug 9, 2023
    Posts:
    22

    But that above code is working fine on Start method and earlier it was also working on click listener also, but now I don't now what happened it is working on Start() method but not on click listener. You can see.
     

    Attached Files:

  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    I've already read this in your original post so I'm not sure why you've mentioned it again. I've no idea what the video is supposed to be showing apart from something moving.

    So you've checked that you're actually calling "Play" with all this debugging you've been doing? It won't matter where you call it from.

    I still think what you're doing above is very odd, It just screams, "make this an animation"! ;)

    You're saying adding this to your script and hitting play doesn't work?
    Code (CSharp):
    1.     private void OnGUI()
    2.     {
    3.         if (GUI.Button(new Rect(10, 10, 150, 100), "Play"))
    4.         {
    5.             Play();
    6.         }
    7.     }
     
    Yoreki likes this.
  8. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    OP.. please just respond with any meaningful answer to the one most important question that has been asked several times. Do you actually call this Play() method you created? There is no Play() function provided by Unity. This is your own function, so you must call it yourself. In the code provided you dont do that. That's why you have been asked several times now..
    .. which you basically ignored. You put the StartCoroutine in a function that is never called. Thus the code of that coroutine will not be executed. It's as simple as that. If you execute code, the code will do what it is implemented to do.

    If you put a simple
    Code (CSharp):
    1. Debug.Log("Is this getting called?");
    into your Play() method and told us whether that shows up in the console, that would be all we want to know. Unless you call Play() from some other script, which has a reference to BatRotate, it wont tho.
     
    Last edited: Sep 26, 2023
    Chubzdoomer and MelvMay like this.