• 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 /
avatar image
0
Question by Meli497 · Dec 20, 2016 at 03:18 PM · 2d-platformer

Button doesn't react properly

Hello, I'm creating a 2D Endless Scroller and I'm experiencing trouble when making the Player choose between all the characters he had purchased from the ingame shop. Basically whenever I click on the "Change Dog" Button, sometimes it right away switches and sometimes it first needs 10 clicks or more to respond. This is how the function works to change the dog, explanation coming below: (Btw, the code is still a bit dumb, trying to create a more cleaner version afterwards i find the solution of my problem)

 public void changeChar()
     {
         List<int> keys = new List<int>();
         for(int i=0;i<dogsBought.Count;i++)
         {
             keys.Add(i);  
         }

 
        for(int i=0;i<dogsBought.Count;i++)
         {
             if(currentChara == i&&currentChara!=dogsBought.Count-1)
             {
                 dogs.sprite = dogsBought[keys[i+1]];
                 break;
             }
 
             if(currentChara == i && currentChara == dogsBought.Count-1)
             {
                 dogs.sprite = dogsBought[keys[0]];
                 break;
             }
 
 
 
         }
         
         if (dogs.sprite == dog1)
         {
             moves.SetBool("retriever", true);
             moves.SetBool("corgi", false);
             moves.SetBool("shibe", false);
             moves.SetBool("husky", false);
         }
 
         if(dogs.sprite == dog2)
         {
             moves.SetBool("retriever", false);
             moves.SetBool("corgi", false);
             moves.SetBool("shibe", true);
             moves.SetBool("husky", false);
         }
 
         if (dogs.sprite == dog3)
         {
             moves.SetBool("retriever", false);
             moves.SetBool("corgi", true);
             moves.SetBool("shibe", false);
             moves.SetBool("husky", false);
         }
 
         if (dogs.sprite == dog4)
         {
             moves.SetBool("retriever", false);
             moves.SetBool("corgi", false);
             moves.SetBool("shibe", false);
             moves.SetBool("husky", true);
         }
 
 
 
     }

dogsBought: This is a Sprite List which is created and filled in Start(). Whenever a player purchases another character in the shop code, I set a playerpref to receive in the start method and to fill the sprite in the list

currentChara: By using currentChara = dogsBought.IndexOf(currentChar); in the Update() Method, I make sure this int always is filled with the index of the current Character selected

currentChar: Same as above but only with the current Sprite

moves: Animator

dog1,2,3,4 = Variables which reference to the sprite which are attached to this script

keys: a List for the index variables

However, I do not think that the entire code is incorrect since the button is working but it is reacting very poorly. For every 5 clicks or something the character switches. But at least in right order.

Anyone can find the mistake?

BR

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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Weird physics issue after disabling and re-enabling player gameobject. 3 Answers

2d shooting problem 1 Answer

How to Image Swap on Collision 1 Answer

2D Box Collider / Sprite change direction issue 1 Answer

How to add questions inside a platformer game ? 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