Search Unity

is it possible to call a function located in a collider

Discussion in 'Scripting' started by kevinlettuce, Jul 12, 2014.

  1. kevinlettuce

    kevinlettuce

    Joined:
    Jun 19, 2014
    Posts:
    7
    okay i am trying to come up with a dialogue system where the words can easily be replaced for different characters

    the script itself works all right but the idea of it is to attach the script to a bunch of trigger colliders with the same tag, and when the player character enters the trigger, i'm trying to have it run a function from the script attached to the exact game object acting as the trigger. this is because i have a string value i can change there for each game object, and the string value is what the gui text displays.

    right now it doesn't work unless i define the game object holding the script beforehand. this means i would only be able to interact with one character/gameobject and the game would freeze up whenever i would enter the trigger for an undefined game object.

    i'm essentially just trying to figure out how to call the script located in the object with which i'm colliding.

    here is the relevant code i have attached to the player character:

    Code (JavaScript):
    1. var talkScript : TalkScript;
    2.  
    3. function OnTriggerEnter (other : UnityEngine.Collider)
    4. {  
    5.     if (other.gameObject.tag == "inTheZone")
    6.     {
    7.         talkScript.TALKMAN();
    8.     }
    9. }
    10.  
    11. function OnTriggerExit (other : UnityEngine.Collider)
    12. {      
    13.     if (other.gameObject.tag == "inTheZone")
    14.     {
    15.         talkScript.farewellFriendWhomSpeak();
    16.     }
    17. }
    18.  
    "TALKMAN" and "farewellFriendWhomSpeak" are just functions in the script "TalkMan" that in theory lets me change the text.
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Code (csharp):
    1. other.gameObject.GetComponent<MyTrigger>().MyFunction()
    This?

    You know, that "other" variable passed on contains all the info needed.
     
  3. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548


    I guess this script is attached to the player so you can do this by simply using GetComponent on the "other" Collider variable.

    Code (JavaScript):
    1. function OnTriggerEnter(other : Collider){
    2. if(other.gameObject.tag == "inTheZone"){
    3. other.GetComponent(TalkScript).TALKMAN();
    4. }
    5. }
     
  4. kevinlettuce

    kevinlettuce

    Joined:
    Jun 19, 2014
    Posts:
    7

    Oh geez thank you. I had been trying to shoehorn gameObject into the GetComponent function
     
  5. unity_Z1_LVfieVf17Jw

    unity_Z1_LVfieVf17Jw

    Joined:
    Mar 28, 2020
    Posts:
    2
    How can I do this in C#?
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Do NOT Necro a 7 year old thread with no information. Not only is it extremely unhelpful, it's also against the Unity Community Code of Conduct.

    Please start your own threads, they're free and easy.

    Thanks.
     
    Bunny83 likes this.
  7. miscritslakshay58

    miscritslakshay58

    Joined:
    Dec 20, 2019
    Posts:
    1
    yes it is pretty easy
    1. save your collider as gameobject using:
    enemy=collision.collider.gameObject;
    2. create variable of your script as enemy can have multiple scripts.
    enemy_destroy damage = (enemy_destroy)enemy.GetComponent(typeof(enemy_destroy))
    where enemy_destroy is name of the script name
    3. damage.takedamage(), just call your public function from here.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Please see my necro comment above.

    It's good to try to help but honestly, this thread has outlived its usefulness. Doing this is one of the most basic things you can do (grab a component) and is covered by docs/tutorials a plenty. Your code isn't really a good recommendation or at least it's a very verbose way of doing things:

    Code (CSharp):
    1. enemy=collision.collider.gameObject;
    2. enemy_destroy damage = (enemy_destroy)enemy.GetComponent(typeof(enemy_destroy));
    3. damage.takedamage();
    ... should be ...
    Code (CSharp):
    1. enemy = collision.gameObject;
    2. var damage = gameObject.GetComponent<enemy_destroy>();
    3. if (damage != NULL) damage.takedamage();