Playing specific sounds when an object is revealed
I have completed a tutorial on how to make a memory card game from a book I got (Unity in Action) everything works great, I can play the game and match all the cards (8 animals) and score points. I can even reset the game and start all over, and all the cards are randomly sorted out.
But I want to go beyond the tutorial and put some sound effects in the game to make it more fun. I get that you can play sounds on mouse clicks and stuff, but I want to play sounds when specific cards are revealed. Duck card appears and you hear a quacking sound.
I think the code I wrote is based off element id's so my first thought was when a card id x is revealed x sound is played, but I have no idea how to approach that idea and put it into a working code.
I'm not sure if I would start a new C# script or add to my existing sceneController script. Any help would be appreciated. Thank you.
This is my scene controller script for the game. I think I would have to add something here.
using UnityEngine;
using System.Collections;
public class SceneController : MonoBehaviour {
public const int gridRows = 4;
public const int gridCols = 4;
public const float offsetX = 4f;
public const float offsetY = 3.85f;
private MemoryCard _firstRevealed;
private MemoryCard _secondRevealed;
private int _score = 0;
public bool CanReveal
{
get
{
return _secondRevealed == null;
}
}
public void CardRevealed(MemoryCard card)
{
if (_firstRevealed == null)
{
_firstRevealed = card;
}
else
{
_secondRevealed = card;
StartCoroutine(CheckMatch());
}
}
[SerializeField]
private TextMesh scoreLabel;
private IEnumerator CheckMatch()
{
if (_firstRevealed.id == _secondRevealed.id)
{
_score++;
scoreLabel.text = "Score: " + _score;
}
else
{
yield return new WaitForSeconds(1f);
_firstRevealed.Unreveal();
_secondRevealed.Unreveal();
}
_firstRevealed = null;
_secondRevealed = null;
}
[SerializeField]
private MemoryCard originalCard;
[SerializeField]
private Sprite[] images;
void Start()
{
Vector3 startPos = originalCard.transform.position;
int[] numbers = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7};
numbers = ShuffleArray(numbers);
for (int i = 0; i < gridCols; i++)
{
for (int j = 0; j < gridRows; j++)
{
MemoryCard card;
if (i == 0 && j == 0)
{
card = originalCard;
}
else
{
card = Instantiate(originalCard) as MemoryCard;
}
int index = j * gridCols + i;
int id = numbers[index];
card.SetCard(id, images[id]);
float posX = (offsetX * i) + startPos.x;
float posY = -(offsetY * j) + startPos.y;
card.transform.position = new Vector3(posX, posY, startPos.z);
}
}
}
void Update()
{
int[] numbers = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 };
}
private int[] ShuffleArray (int[] numbers)
{
int[] newArray = numbers.Clone() as int[];
for (int i = 0; i <newArray.Length; i++)
{
int tmp = newArray[i];
int r = Random.Range(i, newArray.Length);
newArray[i] = newArray[r];
newArray[r] = tmp;
}
return newArray;
}
public void Restart()
{
Application.LoadLevel("MEMORY GAME");
}
}
Your answer
Follow this Question
Related Questions
Playing specific sounds when an object is revealed 1 Answer
identify arrayed game objects 2 Answers
Button on win? 2 Answers
clone a game object and use it 0 Answers