Search Unity

Fear into the trigger

Discussion in 'Scripting' started by massiwo, Nov 29, 2015.

  1. massiwo

    massiwo

    Joined:
    Dec 26, 2014
    Posts:
    12
    Hi eveybody,

    I'm working on a horror game and i'm blocked on a script...

    Hum... I do not know where to start. To be simple, i try to do a script which activate what i want when a game object enter into a area/trigger. For example, when i say "what i want" i mean : "display an UI Text" or "play a sound" or "move a game object", etc.

    I thought to make a script for each action and a global script that calls these scripts depending on what I need to do (sound, movement, etc.). But it may do a lot of independent script and as a developer I try to make that the most dynamic possible, to be reused.

    Do you see what i try to do ?


    Sorry if my english is bad, i'm a french developer ;)
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    look up "unityevent"
     
    Kiwasi likes this.
  3. massiwo

    massiwo

    Joined:
    Dec 26, 2014
    Posts:
    12
    I already searched this problem, but i came upon codes which explain "the syntax of a trigger function". This is not what I want...

    This not the content of the code for each script than i want, it is just a help into development logic and have a dynamic code.

    In other words, without writing the complete code, for exemple, how would you have done to create a script that : moves a gameobject, activates a sound and destroy the gameobject once the ride finished, simultaneously.

    Should i create a script for each of his actions and a "global script" attached on the gameobject which use the needed actions ?
    Or an other solution, more simple and more beautiful ?

    I don't know if i explain well...
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    look up "unityevent"
     
    Kurt-Dekker likes this.
  5. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Define a UnityEvent event in your activator script.

    Code (CSharp):
    1.  
    2. using UnityEngine.Events;
    3.  
    4. public UnityEvent ObjectTriggered;
    Then in you can raise an event when required like so:

    Code (CSharp):
    1. void SomethingTriggeredMe () {
    2.         ObjectTriggered.Invoke();
    3.     }
    Your other scripts can subscribe to this like so, they will need a reference to your trigger object:

    Code (CSharp):
    1. void OnEnable()
    2.     {
    3.         myTriggerObject.ObjectTriggered.AddListener(OnObjectWasTriggered);
    4.     }
    5.  
    6.     void DisableEnable()
    7.     {
    8.         myTriggerObject.ObjectTriggered.RemoveListener(OnObjectWasTriggered);
    9.     }
    10.  
    11.     // This is where you would implement code in response to your trigger object being 'triggered'
    12.     void OnObjectWasTriggered()
    13.     {
    14.         Debug.Log ("The object was triggered");
    15.     }
    Hope this helps.
     
    Last edited: Nov 30, 2015
  6. massiwo

    massiwo

    Joined:
    Dec 26, 2014
    Posts:
    12
    Ok, thanks for this helps.
    i m going to try this

    Confirm me :
    So if i understand it, if i replace "SomethingTriggeredMe" by "OnTriggerEnter", the trigger object gonna call the "Invok" which execute the "OnObjectWasTriggered" in each others scripts ?