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

Function from another script is not called

Discussion in 'Scripting' started by FalconJ, Mar 16, 2015.

  1. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    I have 2 scripts (mouseController and Mover). And mouseController script needs to call function from Mover script.

    Here's the code :
    1. mouseController script
    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D collider)
    2.     {
    3.         Mover Mover = GetComponent<Mover> ();
    4.         Mover.StopLevel ();
    5.     }
    2. Mover script
    Code (CSharp):
    1. public void StopLevel(){
    2.         Debug.Log ("StopLevel called");
    3.     }
    The log is not called so I assume the function is not called.
    What's wrong with that?
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    First thing I would try is adding a Debug.Log() in the OnTriggerEnter2D function of mouseController, too. Make sure the collision is being detected.

    I don't see anything that jumps out as being incorrect in the code. Move is attached to the same object as mouseController, right?
     
  3. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    The collision is detected. But, the mover is not attached on the same object. Is that possible to still call it?
     
  4. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Just a thought - but first check what Schneider said. This would be an option #2 to try.

    The name of the class is Mover.
    The name of the variable is called Mover.

    Ideally, this should be fine, but maybe change the name of the variable to a lowercase m and see what happens? The compiler should be able to resolve this (I think), but maybe it needs a clean rebuild or something. It may be trying to call StopLevel() as a static function of the class Mover rather than a member of an instantiated Mover class.

    Code (CSharp):
    1.     void OnTriggerEnter2D(Collider2D collider)
    2.         {
    3.             Mover mover = GetComponent<Mover> ();
    4.             mover.StopLevel ();
    5.         }
    6.  
     
  5. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Ah, this would be a problem. The way your code is written, its attempting to get a component off the same game object. Are you trying to get it off the game object that collided with the "main" game object?

    Code (CSharp):
    1.     void OnTriggerEnter2D(Collider2D collider)
    2.         {
    3.             Mover mover = collider.gameObject.GetComponent<Mover> ();
    4.             mover.StopLevel ();
    5.         }
     
    FalconJ and Schneider21 like this.
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    Yep. This should do it.
     
    FalconJ and knr_ like this.
  7. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    That worked! Thanks rnakrani and schenider21!
     
    knr_ and Schneider21 like this.