Lesson 4

Table of Contents

Assigning and Posting a Wwise-Type Event

[Note]

Before engaging this section, you must complete the preceding section: Creating a Wwise-Type Event property.

To post the Event, let's use the Start() function, which will be called one time on executing the game. When writing your scripts, to access the functions, variables or properties of a class property, you can type in a period '.' after the property name. As you may have noticed from the previous section, this will make Visual Studio display a list of options related to the class for your convenience.

  1. In the Start() function, type MyEvent followed by a period '.'.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
        
    public class PostWwiseEvent : MonoBehaviour {
        public AK.Wwise.Event MyEvent;
        // Use this for initialization.
        void Start () {
            MyEvent.
        }
        
        // Update is called once per frame.
        void Update () {
        
        }
    }

    This indicates to Unity that you want to do something with your Event.

    Once you hit period, a dropdown menu should appear. In that dropdown, Visual Studio has collected a list of available functions and variables available to the AK.Wwise.Event class that you can use.

    If the dropdown is not appearing in your code editor, you can simply type it in manually as in the following step. We will be using the Post() function to play the Event.

  2. Continuing from MyEvent., type or double-click on Post().

    public AK.Wwise.Event MyEvent;
    // Use this for initialization.
    void Start () {
        MyEvent.Post()
    }

    The function call is not yet complete. By hovering the mouse on the function, you will get a help message of what's missing.

    It gives you a lot of information but, boiling it down, it needs at least a game object on which you want to post the sound. Because in our case we will be posting the sound on the same game object that the script is attached to, the Player game object, you can just write the following, without naming a particular game object.

  3. Inside the Post() parentheses type gameObject.

    public AK.Wwise.Event MyEvent;
    // Use this for initialization.
    void Start () {
        MyEvent.Post(gameObject)
    }

    And lastly, end the line by adding a semicolon. As this is the last step in constructing your script, this is also where you would most likely skim through the script and verify that you have remembered to add semicolons and ending parentheses.

  4. Continuing from MyEvent.Post(gameObject), add a semicolon (;).

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
        
    public class PostWwiseEvent : MonoBehaviour {
        public AK.Wwise.Event MyEvent;
        // Use this for initialization.
        void Start () {
            MyEvent.Post(gameObject);
        }
        
        // Update is called once per frame.
        void Update () {
        
        }
    }

    That's it! You can now assign an Event in the Inspector and the Event will be posted on the Player game object when starting the game. Let's try it out.

  5. Save the script, and in the Unity Inspector, go to PostWwiseEvent, click the MyEvent property drawer, and select the Destruction_Rock_Destroy Event.

    Notice that the property drawer automatically opens to the Events folder in your Wwise project.

  6. Click Play and listen for the Event.

    Once the game has loaded, you will hear the sound of a large rock impacting on the player, so you have successfully posted an Event from a script. You might be thinking, "Why do we need to add a rock destruction sound on the player?" The Destruction_Rocks Event is simply used to test whether your script implementations are working. Furthermore, you will be using the same script in the following section, Posting Events from Animations, to build on what you just created. You may now exit Play mode.

Writing scripts is an essential part of game development, and should you do any future work in Unity, you will definitely benefit from knowing and working with scripts.