Tag

inspector

Our First Sound

By | Uncategorized | No Comments

Finally, we are ready to play back our first sound in Unity!

    1. Create another new empty GameObject in Unity, by right clicking in the Hierarchy and selecting Create Empty.
    2. In the Unity Project window, under the Assets folder, create a new top level folder called Audio_Scripts. This will contain all C# scripts related to audio.
    3. Right click in the Audio_Scripts folder, and create a new C# script called Play_Start. Insert the following code and save the script.
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      
      public class Play_Start : MonoBehaviour {
      
          //public string for Wwise Event name
          public string wwiseEvent;
          //public GameObject defines the GameObject this component is attached to
          public GameObject location;
      
          //everything in void Start happens upon starting the game
          void Start ()
          {
              //calls the wwise event
              AkSoundEngine.PostEvent(wwiseEvent, location);
          }
      }
      //https://audioprogblog.com/
      
    4. Now we can place the script on a GameObject in the hierarchy. Create an empty GameObject in the hierarchy called ‘Audio’, and drag the Play_Start script into the inspector. We will see the script appear as a component, and we will see that our public wwiseEvent and location variables are accessible directly in the inspector. Enter the name of the Wwise Event in the wwiseEvent field (In my case this is ‘Play_Ambience’). Drag the GameObject we want the sound to emit from into the location field. As this is a 2D sound, I use the FPSController.
    5. Enter play mode! If the sessions have been set up correctly and the event name in the inspector matches your Wwise event name, our sound should play back as soon as the game starts.

Troubleshooting

Some common errors that are thrown around this time include:

Wwise Event ID Not Found – Ensure the Wwise session has been saved, the SoundBanks have been generated in Wwise and the AkBank component is on a GameObject and being loaded on start. Also make sure the spelling of the Wwise Event and the wwiseEvent field in the inspector in Unity are the same (everything in code is case sensitive). The Wwise Picker tab in Unity also has an option to refresh the project and generate SoundBanks.

#wwise #unity #csharp #inspector