• 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 xChris18 · Jan 14, 2021 at 05:48 PM · arrays

How can you compare the contents of two arrays?

Could anyone help with comparing whether one sequence is equal to a correct sequence of numbers. My code basically lets the player hit 7 notes on a xylophone and after the 7th one it checks the players input with the correct sequence but for some reason it runs both situations, saying it is correct even if the contents of one array do not match the contents of another. Any help would be greatly appreciated.

 public class SCR_Xylophone : MonoBehaviour
 {
     [SerializeField] private int[] correctSequence = { 1, 1, 5, 5, 6, 6, 5 };
     [SerializeField] private int[] playerSequence = { 0, 0, 0, 0, 0, 0, 0 };
     [SerializeField] private Animator anim;
     [SerializeField] private AudioSource incorrectSound;
     [SerializeField] private AudioSource successSound;
     private int index = 0;
 
     void Update()
     {
         //Checks to see whether the players input is the correct input & plays an animation if true
         if (index == 7)
         {
             for (int i = 0; i < correctSequence.Length; i++)
             {
                 if (playerSequence[i] != correctSequence[i])
                 {
                     index = 0;
                     incorrectSound.Play();
                 }
                 else if(playerSequence[i] == correctSequence[i])
                 {
                     StartCoroutine(SuccessSequence());
                     GetComponent<SCR_Xylophone>().enabled = false;
                 }
             }
         }
     }
     //Checks to see what bar has been hit and changes each element in the array & increases the index
     public void AttemptSequence(int barValue)
     {
         switch(index)
         {
             case 0:
                 break;
             case 1:
                 playerSequence[0] = barValue;
                 break;
             case 2:
                 playerSequence[1] = barValue;
                 break;
             case 3:
                 playerSequence[2] = barValue;
                 break;
             case 4:
                 playerSequence[3] = barValue;
                 break;
             case 5:
                 playerSequence[4] = barValue;
                 break;
             case 6:
                 playerSequence[5] = barValue;
                 break;
             case 7:
                 playerSequence[6] = barValue;
                 break;
         }
     }
 
     IEnumerator SuccessSequence()
     {
         anim.SetBool("isCorrect", true);
         successSound.Play();
         yield return new WaitForSeconds(1f);
         successSound.Stop();
     }
 }
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 Hellium · Jan 14, 2021 at 06:20 PM

I highly advise you the following rework.

Keep as many things private as possible. If anyone outside of your class can change the values anytime, you loose control over what's going on. The other classes don't need to know how your class works internally. Just add methods to your class your other classes will just need to call, and do the logic inside those methods.

 public class SCR_Xylophone : MonoBehaviour
 {
     // SerializeField allows you to keep the variable private while being able to edit it in the inspector
     [SerializeField] private int[] correctSequence = { 1, 1, 5, 5, 6, 6, 5 };
     [SerializeField] public Animator anim;
     [SerializeField] public AudioSource incorrectSound;
     [SerializeField] public AudioSource successSound;
     private int _playerInputIndex = 0;

     // Call this method outside of your class
     // instead of manipulating index and playerSequence 
     public void AddPlayerInput(int value)
     {
         // Reset if incorrect value provided
         if(value != correctSequence[_playerInputIndex])
         {
             _playerInputIndex = 0;
             incorrectSound.Play();
             return;
         }
 
         // Correct input
         _playerInputIndex++;
 
         // Last input reached, meaning all the values are correct
         if(_playerInputIndex == correctSequence.Length)
         {
             _playerInputIndex = 0;
             StartCoroutine(SuccessSequence());
             GetComponent<SCR_Xylophone>().enabled = false;
         }
     }
Comment
Add comment · Show 1 · 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 xChris18 · Jan 24, 2021 at 05:28 PM 0
Share

Hey, just wanted to say a big thank you for this, the puzzle I have made now works perfectly with the new script, this has been a pain for a bit and it's great to have that weight off of my shoulders, thank you again.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

157 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 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 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

Why does this loop throw an error if int questComplete rises above 1? 0 Answers

Managing In-Game Variables of Different Data Types 1 Answer

Defining a number of gameobjects from another gameobject 0 Answers

animation array trying to play all at once 0 Answers

Noob question about very simple array script 2 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