Project Boost/Audio Source / ADDING A TOUCH OF AUDIO

Problem - Audio does not play after I entered code.

void Start()
{
rigidbody = GetComponent();
audioSource = GetComponent();
}

// Update is called once per frame
void Update()
{
    ProcessInput();
}

private void ProcessInput()
{
    if (Input.GetKey(KeyCode.Space))
    {
        rigidbody.AddRelativeForce(Vector3.up);
        if(!audioSource.isPlaying)
        {
            audioSource.Play();
        }
    }
    if (Input.GetKey(KeyCode.A))
    {
        transform.Rotate(Vector3.forward);
    }
    else if (Input.GetKey(KeyCode.D))
    {
        transform.Rotate(-Vector3.forward);
    }

}

Here’s a pic of my UI

Hi Hector,

Is there a reason why the AudioSource is attached to the camera? Does your code find that AudioSource?

Hi Nina,

Is there a reason why the AudioSource is attached to the camera? BASED ON THE VIDEO TUTORIAL, I THOUGHT THE AUDIO SOURCE COMPONENT WAS SUPPOSE TO BE ON THE MAIN CAMERA COMPONENT. AM I WRONG?

Does your code find that AudioSource? I’M NOT SURE.

Many thanks for your help. :slight_smile:

Hector

GetComponent looks for a component attached to the same game object as your script component. If the script is attached to the Rocket Ship game object, add an AudioSource to that game object. Then test your game again. Did this work?

Yes.

  1. Audio Listener attached to Main Camera.
  2. Added Audio Source component to Game Object/ Rocket Ship
  3. From Project view, loaded Audio clip into Audio source field ( audio clip)

Thanks!

In your last screenshot, is that the Inspector of the Nose Cone or the Rocket Shop? I’m wondering if there are multiple Rocket scripts and Audio Sources in your scene.

This topic was automatically closed after 13 days. New replies are no longer allowed.

Privacy & Terms