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

How to call method from other class

Discussion in 'Scripting' started by AAppDev, Sep 2, 2015.

  1. AAppDev

    AAppDev

    Joined:
    Aug 21, 2015
    Posts:
    17
    I tried this
    Code (CSharp):
    1. public class Obstacle : MonoBehaviour {
    2.     PlayerContorler pc = new PlayerContorler ();
    3.     void OnTriggerEnter2D(Collider2D collider){
    4.         if (collider.gameObject.name == "Player") {
    5.             pc.death();
    6.     }
    7.     }}
    Code (CSharp):
    1.  public  void death(){
    2.         //die
    3.         Destroy (this.gameObject);
    4.  
    5.  
    6. }
    but that doesn't work
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    You need to call the method on the colliders PlayerController like so:
    Code (CSharp):
    1. public class Obstacle : MonoBehaviour
    2. {
    3.     void OnTriggerEnter2D(Collider2D collider)
    4.     {
    5.         if (collider.gameObject.name == "Player")
    6.         {
    7.             var pc = collider.GetComponent<PlayerContorler>();
    8.             pc.death();
    9.         }
    10.     }
    11. }
     
  3. 1Piotrek1

    1Piotrek1

    Joined:
    Mar 14, 2014
    Posts:
    130
    Use
    collider.SendMessage ("death ")
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This may help with the general concepts. The key piece you are missing is GetComponent.

     
  5. georetro

    georetro

    Joined:
    Jan 18, 2013
    Posts:
    218
    @1Piotrek1 SendMessage is ok to use once in a blue moon, but it's expensive so don't use it often!
     
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    It's not horrible but it does get worse if you have a lot of components because it tries to send the message to every component on the GameObject.

    It's practical for controlled scenarios, basically the same as GetComponent<>() when you aren't caching it. If you could cache a GetComponent and reuse it then that would always be better - assuming you are only targetting one component.
     
    georetro likes this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The key difference between SendMessage and GetComponent has nothing to do with the performance. It's all to do with the information you have about the other GameObject.

    If you know a specific component exists, then get component is ideal. On the other hand if there might be more or less then one receiver, SendMessage is better.

    Note that in most cases SendMessage should be replaced by the faster and less error prone ExecuteEvents. Abandoning compile time checking and calling methods by strings is always a risky idea.
     
  8. AAppDev

    AAppDev

    Joined:
    Aug 21, 2015
    Posts:
    17
    Ok I use now this :
    Code (CSharp):
    1.     PlayerContorler pc;
    2.     void Awake(){
    3.         GameObject player = GameObject.Find ("Player");
    4.          pc = player.GetComponent<PlayerContorler> ();
    5.     }
    6.     void OnTriggerEnter2D(Collider2D collider){
    7.         if (collider.gameObject.name == "Player") {
    8.             pc.death();
    9.     }
    10.     }
    Thanks for the replies :D