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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Cannot step into class / code not executing

Discussion in 'Scripting' started by rhenretta, Jun 27, 2016.

  1. rhenretta

    rhenretta

    Joined:
    Nov 6, 2013
    Posts:
    2
    I have a MonoBehavior class attached to a model. Inside the Start() method, I instantiate the other class. I've significantly truncated the below code to show only the relevant parts.

    Code (CSharp):
    1. public class AlienController : MonoBehaviour {
    2.     public API api;
    3.  
    4.     // Use this for initialization
    5.     void Start () {
    6.         api = new API();
    7.         api.OnNewPosts += NewPosts;
    8.     }
    9.  
    10.     void OnSelect()
    11.     {
    12.         getPosts();
    13.     }
    14.  
    15.     private void getPosts()
    16.     {
    17.         api.RequestMorePosts();
    18.     }
    19.  
    20.     public void NewPosts(API.Post[] posts)
    21.     {
    22.         throw new NotImplementedException();
    23.     }
    24. }

    Code (CSharp):
    1. public class API {
    2.     /* Post class, Response class and subclasses defined here */
    3.  
    4.     public delegate void NewPosts(Post[] posts);
    5.  
    6.     private Post[] _posts;
    7.  
    8.     public event NewPosts OnNewPosts;
    9.  
    10.     public API()
    11.     {
    12.     }
    13.  
    14.     public virtual IEnumerator RequestMorePosts()
    15.     {
    16.         Debug.Break();
    17.         Debug.Log("Requesting new posts");
    18.         WWW www = new WWW(BASE_URL);
    19.         yield return www;
    20.  
    21.         Debug.Log("Api returned");
    22.         var posts = new ArrayList();
    23.         /* Deserialization and translation code here */
    24.  
    25.         if (OnNewPosts!=null)
    26.         {
    27.             OnNewPosts((Post[])posts.ToArray(typeof(Post)));
    28.         }
    29.     }
    30. }
    31.  
    When I set a breakpoint in AlienController.getPosts() it stops execution as expected. api is confirmed to be instantiated. When I try to step into api.RequestMorePosts(), it fails with
    Step into: Stepping over non-user code 'API.RequestMorePosts'
    Step into: Stepping over non-user code 'API.<RequestMorePosts>d__18..ctor'

    My breakpoints inside that class never hit, and my Debug.log / Debug.break statements never hit either. The event also never fires. It is literally giving me nothing to debug with. I'm new to Unity, but am well versed in C#. Any ideas?
     
  2. luke_2

    luke_2

    Joined:
    Nov 20, 2012
    Posts:
    29
    I'd hazard a guess it's because you are calling RequestMorePosts directly, yet it is a coroutine (you haven't included your import statements so not completely sure, also haven't tried recreating to see what would happen if a coroutine is called directly).
    See
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
    for how to start a coroutine & see if that makes a difference.
     
  3. rhenretta

    rhenretta

    Joined:
    Nov 6, 2013
    Posts:
    2
    Oh, thank you! That has been driving me nuts. I'm still not sure what StartCoroutine actually does, but I know it works now.