Question by
ltrout1999 · Dec 13, 2015 at 07:43 PM ·
c#error
MuteController Code Error?
I am creating a simple script that will toggle the audio depending on if tab is pressed. Here is the code I have:
using UnityEngine;
using System.Collections;
public class MuteController : MonoBehaviour {
var muted = false;
// Update is called once per frame
public void Update()
{
if (Input.GetKey("tab") && (muted == false))
AudioListener.volume = 0;
muted == true;
else if (Input.GetKey("tab") && (muted == true))
AudioListener.volume = 5;
muted == false;
}
}
But, when I save it and go back to Unity, it gives me this error:
Assets/MuteController.cs(13,30): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as statements.
Could anyone help me out? I've just started learning C# a few weeks back.
EDIT: I have even tried this code:
using UnityEngine;
using System.Collections;
public class MuteController : MonoBehaviour {
bool isMuted = false;
// Update is called once per frame
public void Update()
{
if (Input.GetKey("tab") && (isMuted == false))
{
AudioListener.volume = 0;
isMuted = true;
}
if (Input.GetKey("tab") && (isMuted == true))
{
AudioListener.volume = 1;
isMuted = false;
}
}
}
After I tried this new code, it doesn't give me an error, but it doesn't toggle the audio at all. EDIT FINAL: I got it to work on the final code. I had to change the second if statement to else if. Works perfectly!
Comment