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
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. Spoiler: My idea 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...
Define a UnityEvent event in your activator script. Code (CSharp): using UnityEngine.Events; public UnityEvent ObjectTriggered; Then in you can raise an event when required like so: Code (CSharp): void SomethingTriggeredMe () { ObjectTriggered.Invoke(); } Your other scripts can subscribe to this like so, they will need a reference to your trigger object: Code (CSharp): void OnEnable() { myTriggerObject.ObjectTriggered.AddListener(OnObjectWasTriggered); } void DisableEnable() { myTriggerObject.ObjectTriggered.RemoveListener(OnObjectWasTriggered); } // This is where you would implement code in response to your trigger object being 'triggered' void OnObjectWasTriggered() { Debug.Log ("The object was triggered"); } Hope this helps.
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 ?