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 ~~) {~~
~~ bird.GetComponent ().enabled = false;~~
~~ } else {~~
~~ bird.GetComponent ().enabled = true;~~
~~ }~~
~~ }~~
~~ GameObject.Find (“MenuBird”).GetComponent().enabled = false;~~
GameObject.Find (skins ).GetComponent ().SetTrigger(“doFlap”);
~~ }~~
~~ void OnMouseDown() {
GameObject.Find (skins ).GetComponent ().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 ).GetComponent ().enabled = true;
GameObject.Find (skins ).GetComponent ().SetTrigger (“doFlap”);
~~ PlayerPrefs.SetInt (“Skin”, s);
Debug.Log ("Trigger: " + gameObject.name + "
Skin: " + 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.