• 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 dotaxis · Jul 17, 2016 at 09:41 AM · onmousedownboxcollider2dincrement

Problem With onMouseDown and Integer Increment/Decrement

I'm having the strangest problem with my character selection script. I have box colliders on two arrows and they detect clicks properly but don't update the integer properly. It always registers a click, but doesn't do anything, and sometimes it increases or decreases by 2 instead of 1.

Here's my script (yes, it's a flappy bird rip-off, shh):

 using UnityEngine;
 using System.Collections;
 
 public class SkinSelect : MonoBehaviour {
     string[] skins = {
         "DefaultBird",
         "ZombieBird",
         "RedBird"
     };
     int s;

     void Start() {
         s = PlayerPrefs.GetInt ("Skin", 0);
         foreach (GameObject bird in GameObject.FindGameObjectsWithTag("Player")) {
             if (bird.name != skins [s]) {
                 bird.GetComponent<SpriteRenderer> ().enabled = false;
             } else {
                 bird.GetComponent<SpriteRenderer> ().enabled = true;
             }
         }
         GameObject.Find ("MenuBird").GetComponent<SpriteRenderer>().enabled = false;
         GameObject.Find (skins [s]).GetComponent<Animator> ().SetTrigger("doFlap");
     }
     
     void OnMouseDown() {
         GameObject.Find (skins [s]).GetComponent<SpriteRenderer> ().enabled = false;

         if (gameObject.name == "arrowLeft") {
             if (s < 0)
                 s--;
             else
                 s = skins.Length - 1;
         } else if (collider.gameObject.name == "arrowRight") {
             if (s < skins.Length - 1)
                 s++;
             else
                 s = 0;
         }
 
         GameObject.Find (skins [s]).GetComponent<SpriteRenderer> ().enabled = true;
         GameObject.Find (skins [s]).GetComponent<Animator> ().SetTrigger ("doFlap");
         PlayerPrefs.SetInt ("Skin", s);
         Debug.Log ("Trigger: " + gameObject.name + "\nSkin: " + s);
     }
 
 
 }
 


What's odd is that the log will show that the correct arrow is triggered but s will not update properly. For example: Trigger: arrowLeft Skin: 0

 Trigger: arrowLeft
 Skin: 2
 
 Trigger: arrowRight
 Skin: 2
 
 Trigger: arrowLeft
 Skin: 0

The results are seemingly inconsistent, though. I've tried incrementing and decrementing differently, but seriously why is this an issue at all?

If I change the script to only change s if it's between 0 and 2, after cycling through all the skins once, it'll skip the middle one and keep jumping between 0 and 2. I don't understand at all.

OnMouseUp and OnMouseUpAsButton produce the same behaviour.

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

Answer by dotaxis · Jul 17, 2016 at 10:57 AM

I solved this problem on my own. Because I had my SkinSelect script attached to two different GameObjects (each arrow), each arrow had its own instance of the script and the value at s was being reloaded without this being reflected on screen. I may have been able to solve this in a simpler way, but I chose to attach the script to their parent (empty) GameObject and use my OnClick script to call a function that handles updating s.

SkinSelect.cs:

 using UnityEngine;
 using System.Collections;
 
 public class SkinSelect : MonoBehaviour {
     string[] skins = {
         "DefaultBird",
         "ZombieBird",
         "RedBird"
     };
     string[] skinNames = {
         "Default",
         "Zombie",
         "Red"
     };
     public int s;
 
     void Start() {
         s = PlayerPrefs.GetInt ("Skin", 0);
         foreach (GameObject bird in GameObject.FindGameObjectsWithTag("Player")) {
             if (bird.name != skins [s]) {
                 bird.GetComponent<SpriteRenderer> ().enabled = false;
             } else {
                 bird.GetComponent<SpriteRenderer> ().enabled = true;
             }
         }
         GameObject.Find ("MenuBird").GetComponent<SpriteRenderer>().enabled = false;
         GameObject.Find (skins [s]).GetComponent<Animator> ().SetTrigger("doFlap");
         GameObject.Find ("BirdNameText").GetComponent<GUIText> ().text = skinNames [s];
     }
         
 
     public void ChangeSkin(string arrowPressed) {
         GameObject.Find (skins [s]).GetComponent<SpriteRenderer> ().enabled = false;
 
         switch (arrowPressed) {
         case "arrowLeft":
             if (s > 0)
                 s--;
             else
                 s = skins.Length - 1;
             break;
         case "arrowRight":
             if (s < skins.Length - 1)
                 s++;
             else
                 s = 0;
             break;
         }
 
         GameObject.Find (skins [s]).GetComponent<SpriteRenderer> ().enabled = true;
         GameObject.Find ("BirdNameText").GetComponent<GUIText> ().text = skinNames [s];
         GameObject.Find (skins [s]).GetComponent<Animator> ().SetTrigger ("doFlap");
         PlayerPrefs.SetInt ("Skin", s);
         //Debug.Log ("Trigger: " + gameObject.name + "\nSkin: " + s);
     }
 }

onClick.cs: using UnityEngine; using System.Collections;

 public class onClick : MonoBehaviour {
 
     SkinSelect arrowHandler;
     menuHandler menu;
 
     void Start() {
         arrowHandler = GameObject.Find ("Arrows").GetComponent<SkinSelect> ();
         menu = GameObject.Find ("Menu").GetComponent<menuHandler> ();
     }
 
     void OnMouseUpAsButton (){
         if (gameObject.name == "StartButton")
             menu.SelectScene ("OnlyLevel");
         if (gameObject.name == "OptionsButton")
             menu.SelectScene ("Options");
         if (gameObject.name == "BackButton")
             menu.SelectScene ("Menu");
         if (gameObject.name == "ResetScore")
             PlayerPrefs.SetInt ("highScore", 0);
         if (gameObject.name == "arrowLeft")
             arrowHandler.ChangeSkin ("arrowLeft");
         if (gameObject.name == "arrowRight")
             arrowHandler.ChangeSkin ("arrowRight");
         if (gameObject.name == "QuitButton")
             Application.Quit ();
     }
 
 }

If there was a better solution, I'd love to hear it. I also don't know if I should've just edited my question instead of replying but I'm going to leave it open in case there are other solutions.

Comment
Add comment · 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

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

60 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

Related Questions

IsPointerOverGameObject not working with touch input 3 Answers

OnMouseDown not working under Character Conroller 0 Answers

OnMouseDrag() stopped working after few clicks 0 Answers

how to clamp( set max value) the velocity of a 2d rigidbody? 1 Answer

Simple Run Game 2d Death 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