The If Audio is playing then dont play again doesnt work

This Doesn’t seem to work, it functions the same as if there were no !TheAudioSource.isPlaying exists

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rocket : MonoBehaviour
{
    Rigidbody TheRigidbody;
    AudioSource TheAudioSource;
     void Start()
    {
        TheRigidbody = GetComponent<Rigidbody>();
        TheAudioSource = GetComponent<AudioSource>();
    }
    void Update()
    {
        ProcessInput();
    }

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

Hi,

The problem are the if/else-statements. Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? If not, do that, please. If you want to challenge yourself, try to reenact how the program executes your ProcessInput method.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms