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

Functions not being called in during Start()

Discussion in 'Scripting' started by Count MichX, Feb 16, 2015.

  1. Count MichX

    Count MichX

    Joined:
    Feb 16, 2015
    Posts:
    6
    Hello, I am currently working on a personal project and I am coming across a problem in which I have functions caching and initing variables and data within an object's Start() function, however when the project actually runs it seems that ONLY the first function is ever called… Here is the idea of the code :

    Code (CSharp):
    1. bool toUseLater;
    2.  
    3. void Start ()
    4. {
    5.     CacheData();
    6.        
    7.     InitStats();
    8. }
    9.  
    10. protected void CacheData()
    11. {
    12.        //THIS FUNCTION DOES GET CALLED CORRECTLY
    13.  
    14.     Debug.Log("CACHED DATA");
    15.  
    16.          //cache references to important objects
    17. }
    18.    
    19. virtual protected void InitStats() //This function is overridden in an inheriting class
    20. {
    21.        Debug.Log("INITING STATS");
    22.  
    23.         toUseLater = true;
    24. }
    25.  
    26. void Update()
    27. {
    28.         //everything within this IF never runs because toUseLater still has it's initial value of false
    29.         if( toUseLater )
    30.                  //do something
    31. }

    For some reason although Start() calls CacheData(), InitStats() never get's called and I have no idea why that would happen. Any help you guys could give me would be really appreciated, I can't figure out what's going on. Thanks!
     
  2. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    I guess thats because InitStats is overriden and its that one which is called and not
    this one.
     
  3. Count MichX

    Count MichX

    Joined:
    Feb 16, 2015
    Posts:
    6
    No this is in the base class so there's no other version it could be calling at this point
     
  4. Count MichX

    Count MichX

    Joined:
    Feb 16, 2015
    Posts:
    6
    I even just added a debug directly after CacheData() before InitStates() and it is never called.
    Code (CSharp):
    1. void Start()
    2. {
    3.       CacheData();
    4.  
    5.       Debug.Log("THIS SHOULD PRINT");
    6.  
    7.       InitStates();
    8. }
     
  5. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    aah ok..Is there some error thrown when CacheData is executing ?
    Maybe its cause it just crashes there and thus not proceeding
    to the next calls. I would print a Debug.Log line by line after each line
    in CacheData to find out exactly which line its failing.
     
  6. Count MichX

    Count MichX

    Joined:
    Feb 16, 2015
    Posts:
    6
    I commented out everything in CacheData and it still did the same thing
     
  7. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    hmm this is really weird then..can you put a breakpoint in Start and go line by line and
    see what happens ?
     
  8. Count MichX

    Count MichX

    Joined:
    Feb 16, 2015
    Posts:
    6
    I'm using MonoDevelop and I'm not sure how to get the breakpoints to actually work without linking the code to Visual Studio (which I can't do cause I'm using a Mac)
     
  9. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
  10. Count MichX

    Count MichX

    Joined:
    Feb 16, 2015
    Posts:
    6
    I just figured it out. I realized I had been trying to test my code with the inheriting class and I had forgotten that I hadn't called InitStats in that class's Start function. Thanks for the help though!