Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by nsane808 · Aug 16, 2016 at 05:29 AM · gameobjectssound effectscardselement

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");
     }
 
 
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

64 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

The Object you want to instantiate is null. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges