A game about forced loneliness, made by TACStudios
at master 71 lines 3.2 kB view raw view rendered
1# Trigger a Custom Scripting Event from a C# script 2 3You can send or trigger a Custom Scripting Event node in a Script Graph with a C# script instead of a Custom Scripting Event Sender node. For more information on how to create a Custom Scripting Event Sender node, see [Create a Custom Scripting Event Sender node](vs-create-own-custom-event-send-node.md). 4 5> [!NOTE] 6> Before you can trigger a Custom Scripting Event node, you must create your Custom Scripting Event node. [!include[tasks-note-end](./snippets/custom-events/vs-tasks-note-end.md)]. 7 8To trigger an Event from a C# script: 9 101. [!include[open-project-window](./snippets/vs-open-project-window.md)] 11 121. [!include[right-click-project](./snippets/custom-events/vs-right-click-project.md)] 13 141. [!include[create-c-script-project](./snippets/vs-create-c-script-project.md)] 15 161. Enter a name, such as `CodeTriggerCustomEvent`, for the new script file. 17 181. Press Enter. 19 201. [!include[open-new-external-code](./snippets/vs-open-new-external-code.md)] 21 221. In your external editor, copy and paste the following code into your C# script: 23 24 ```csharp 25 using Unity.VisualScripting; 26 using UnityEngine; 27 28 public class CodeTriggerCustomEvent : MonoBehaviour 29 { 30 void Update() 31 { 32 if (Input.anyKeyDown) 33 { 34 //Trigger the previously created Custom Scripting Event MyCustomEvent with the integer value 2. 35 EventBus.Trigger(EventNames.MyCustomEvent, 2); 36 } 37 } 38 } 39 ``` 40 411. [!include[save-script](./snippets/vs-save-script.md)] 42 431. [!include[return-unity](./snippets/vs-return-unity.md)] 44 451. [!include[open-hierarchy-window](./snippets/vs-open-hierarchy-window.md)] 46 471. Do one of the following in the Hierarchy window: 48 - Select an existing GameObject where you want to attach the new script. 49 - Select **Add New** (+) and in the menu, select a new GameObject to add to your scene from any of the available options. You can also right-click anywhere in the Hierarchy window and select the same options in the context menu. 50 511. [!include[open-inspector-window](./snippets/vs-open-inspector-window.md)] 52 531. Select **Add Component**. 54 551. In the Component menu, enter the name of the script file. 56 571. Select it to add it to the GameObject. 58 59 ![An image of the Unity Editor's Inspector window with the Add Component menu open. The search bar has filtered the components displayed and the Code Trigger Custom Event C# script is highlighted.](images/vs-custom-event-add-code-trigger-script-inspector.png) 60 611. Select **Play** from [the Unity Editor's Toolbar](https://docs.unity3d.com/Manual/Toolbar.html) to enter Play mode. 62 631. Press any key on keyboard in the [Game view](https://docs.unity3d.com/Manual/GameView.html). 64 65 Visual Scripting triggers your Event in any Script Graph in the current scene that contains the Custom Scripting Event node. 66 67## Next steps 68 69After you create the script, you can [create a script to listen to your Event](vs-create-own-custom-event-listen-code.md). 70 71You can also [create an Event Sender node](vs-create-own-custom-event-send-node.md) to trigger the Event from another Script Graph or location in the same Script Graph.