Trying to use audio causes errors when trying to use in an 'if' statement
I have spent days stuck on this problem and I'm sure it's a simple solution but my brain is just frying itself right now and I really need help on this one.
What I'm trying to do: When the player presses up on the Dpad on a controller or a key on the keyboard, a sound will play.
The problem: C# is throwing errors. Everything I try from researching this problem throws more errors and the manual isn't helping either.
My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
public class DistractScript : MonoBehaviour
{
public AudioSource Sound;
public AudioClip Distract;
// Start is called before the first frame update
void Start()
{
}
void Distraction()
{
if (Input.GetButton("Y Button"))
{
AudioClip.Play;
Debug.Log("Player used Distraction");
}
}
// Update is called once per frame
void Update()
{
}
}
The error:
Only assignment, call, increment, decrement, await and new object expressions can be used as a statement.
Now, I have tried both AudioClip and AudioSource to see if it's an issue, because I have seen others use audio in statements like this before, so I'm certain my n00b brain is the problem here, because it seems so simple but I keep failing on this and I need someone more experienced to point out where I'm going wrong here.
Answer by Scorpionzz · Mar 06, 2020 at 03:18 PM
Hi,
You should try something like this :
void Update()
{
if (Input.GetKeyDown(KeyCode.Y))
{
Debug.Log("Ykey was pressed.");
}
}
Your answer
Follow this Question
Related Questions
play Audio Array in java 0 Answers
Audio Endless Loop c# 1 Answer
Playing multiple sounds at the same time? 1 Answer
Audio playing at hightspeed and simoultanesly various times! 1 Answer
Stop AudioSource on Trigger!!! 1 Answer