Common Operations¶
Playing a sound¶
Let’s create the following script: “A sound should be played when the user clicks on the object”. This is how you could do that using Brain Builder.
Below, I have created a scene that will serve as a starting point. When the player clicks the box in the middle, a sound named “Complete.wav” (residing in the Sounds folder) should be played.
Step 1: Create an Audio Source component on the box¶
In order for the box to play a sound, it must have an AudioSource component. Add it via the Component/Audio/Audio Source menu item while the component is selected. A small speaker icon should appear on the cube.
Drag the sound to the Audio Clip field of the audio source.
Step 2: Add a Brain to the box¶
Click on the Component/Brain Builder/Brain menu option and enter Play Sound as filename. Click on the big Open Brain Builder button in the bottom of the inspector.
Step 3: Create the script¶
You will need the following blocks in order to play a sound:
- OnMouseUp, found in the Events folder.
- Play(), found in the Unity Builtins/Audio/AudioSource folder.
Snap these together like below:

Like the box says, there is a variable unbound. Drag the cube object from the hierarchy on top of the AudioSource variable:

And that’s it! Play the game and try clicking on the cube to here the sound play.
Playing a sound loaded at runtime¶
Let’s say that we want to load the sound file from resources at runtime instead of design-time.
The first step would be to create a Resources folder and move the Sounds folder into that. Disconnect the audio clip from the audio source on the box by clicking on the clip in the inspector and pressing backspace.
Open up Brain Builder and find the following block:
- Start, found in the Events folder.
- Set (property) to (value), found in the Builtins folder.
- Load(path), found in the Unity Builtins/Unity Player/Resources folder.
- clip, found in the Unity Builtins/Audio/AudioSource folder.
Assemble them to look like this:

As the hint box states, there are two tasks remaining:
- Double click on the path variable and type “Sounds/Complete” including the quotation marks. This will create a string which represents the path to the sound to play.
- Ctrl-shift-drag the Cube block from the other rule into the AudioSource variable.
The end result should look like the following:

Play the game and try it!
Rasing and lowering an elevator¶
A common operation in many games is to open doors and raise/lower elevators when the character is near them. This is the scene we’ll be using to create an elevator using Unity animations and Brain Builder:
The Elevator Floor will move up and down, while the Elevator Hit Box will stay put. When something enters the hit box, the elevator should rise, and then lower again when something exits.
I have created two animations and attached them to the Elevator Floor object. Also, I have remembered to uncheck the “Play automatically” checkbox:
Now, create a brain on the Hit Box object and name it Elevator Controller. Open Brain Builder and locate the Play(animation) block in Unity Builtins/Animation folder, as well as OnTriggerEnter and OnTriggerExit in the Events folder:

Since the animation variable is a string, type “Raise Elevator” in the first one and “Lower Elevator” in the second one (including the quotation marks).
It might seem wierd to have blue blocks to the right, but they will serve perfectly well as actions too. We will just ignore their return value.
Finally, drag the Elevator Floor object from the hierarchy onto the animation variables:

And we’re done! Save, play, and step onto the elevator to enjoy the marvelous view from the top of the big box.