I have a simple pong game with two sound toggles: one for the ‘beep’ sounds when the ball hits the paddles or walls and another for the sound of rain which I want to be on or off. The sound for the beeps works fine and the toggle for the rain (without the sound) worked until I tried to get the audio clip and audio source working then it stops. The Debug.log doesn’t even show the mouse click on the rain toggle now!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class toggleForRain : MonoBehaviour
{
// Start is called before the first frame update
public AudioListener audioListener;
public SpriteRenderer toggleRain;
public SpriteRenderer rainOff;
public AudioClip rain;
void start()
{
rainOff = GameObject.Find("sunCloud").GetComponent<SpriteRenderer>();
toggleRain = GameObject.Find("rainCloud").GetComponent<SpriteRenderer>();
rainOff.enabled = false;
AudioSource audio = GetComponent<AudioSource>();
audio.Pause();
}
void update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 pos = Input.mousePosition;
Collider2D hitcollider = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(pos));
if (hitcollider != null && (hitcollider.CompareTag("cloud")))
{
Debug.Log("cloud");
if (!toggleRain.enabled)
{
toggleRain.enabled = true;
rainOff.enabled = false;
GetComponent<AudioSource>().Play();
}
else
{
toggleRain.enabled = false;
rainOff.enabled = true;
GetComponent<AudioSource>().Pause();
}
}
}
}
}
Here’s the inspector for the image over where the rain toggle is:
I can’t upload it right now. Error parsing the file!