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 17, 2016 at 05:13 PM · soundonclicksound effectscards

Playing specific sounds when an object is revealed

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TBruce · Aug 17, 2016 at 08:24 PM

Here is the way that I would approach this

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 #if UNITY_5_3_OR_NEWER
 using UnityEngine.SceneManagement;
 #endif
 
 [RequireComponent(typeof(AudioSource), typeof(AudioSource))]
 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;
 
     public List<AudioClip> cardSounds = new List<AudioClip>();
 
     // an AudioSource lets you play sounds
     private AudioSource audioSource;
     
     private int maxCardIDValueSize = 16;
     private MemoryCard _firstRevealed;
     private MemoryCard _secondRevealed;
     private int _score = 0;
     private List<int> cardIDValues = new List<int>();
 
     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;
             if ((cardSounds.Count > 0) && (_firstRevealed.id < cardSounds.Count))
             {
                 audioSource.PlayOneShot(cardSounds[_firstRevealed.id]);
             }
         }
         else
         {
             yield return new WaitForSeconds(1f);
 
             _firstRevealed.Unreveal();
             _secondRevealed.Unreveal();
         }
         _firstRevealed = null;
         _secondRevealed = null;
 
         // a yield must be called in any coroutine
         // the first 'if block' above does not have one
         // this ensures that yield is called in the coroutine
         yield return 0;
     }
 
     [SerializeField]
     private MemoryCard originalCard;
 
     [SerializeField]
     private Sprite[] images;
 
     void Start()
     {
         Vector3 startPos = originalCard.transform.position;
         maxCardIDValueSize = (gridRows * gridCols);
 
         ShuffleCardIDValues();
 
         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;
                 if (index < cardIDValues.Count)
                 {
                     int id = cardIDValues[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);
                 }
             }
         }
 
         // get a handle to the audioSource
         audioSource = GetComponent<AudioSource>();
         audioSource.loop = false; // change this to true if you want it to loop
     }
 
     // Update is called once per frame
     void Update()
     {
     }
 
     private void InitializeCardIDList()
     {
         cardIDValues.Clear();
 
         for (int i = 0; i < maxCardIDValueSize; i++)
         {
             cardIDValues.Add(i / 2);
         }
     }
 
     private void ShuffleCardIDValues()
     {
         InitializeCardIDList();

         // Fisher-Yates Card Shuffle
         for (int i = cardIDValues.Count - 1; i > 0; --i)
         {
             int j = (int)UnityEngine.Random.Range(0, i);
             int temp = cardIDValues[i];
             cardIDValues[i] = cardIDValues[j];
             cardIDValues[j] = temp;
         }
     }
 
     public void Restart()
     {
 #if UNITY_5_3_OR_NEWER
         // Application.LoadLevel is deprecated UNITY 5.3 or newer
         // Use SceneManager.LoadScene instead
         SceneManager.LoadScene("MEMORY GAME");
 #else
         Application.LoadLevel("MEMORY GAME");
 #endif
     }
 }

I have tried to make as few changes as possible, but everything is explained below

  1. Application.LoadLevel() is deprecated as of Unity 5.3, SceneManager.LoadScene needs to be used instead.

  2. Use List instead of an Array (more efficient).

  3. Added a second yield statement at the end of the coroutine. This was added because the only yield in the function is within the else part of the if statement which means an error can occur.

  4. Added an AudioSource (to play sounds).

  5. Added a list for the card sound clips (public - they need to be added in the inspector).

Comment
Add comment · Show 4 · Share
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
avatar image nsane808 · Aug 17, 2016 at 08:39 PM 0
Share

Awesome thank you very much. Ill try this out tonight.

avatar image TBruce nsane808 · Aug 18, 2016 at 04:43 PM 0
Share

Did this answer work for you?

avatar image nsane808 TBruce · Aug 18, 2016 at 04:59 PM 0
Share

Yep works great, I didnt catch that you put Using System.Collections.Generic up top so it wasn't working for a bit, but I eventually found it. $$anonymous$$y kiddo will enjoy playing this once I find the rest of the sounds. Thank you again!

Show more comments

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

65 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 avatar image

Related Questions

How can I get my sound array working here? 0 Answers

change sound pitch on object 0 Answers

How to prevent sound effects playing every frame? 0 Answers

AudioSource.PlayOneShot refuses to play any audio 1 Answer

Universal sound control across each scene 0 Answers


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