- Home /
OnMouseDown Firing When I Miss 2DCollider
So, today when I woke up I was playing around with my game and for some reason when I was not clicking the 2DCollider the OnMouseDown() event was firing regardless of where I clicked. So I tried to troubleshoot the problem, but I have had no success. The weird thing is that the first click only triggers the OnMouseDown() event when I hit the 2DCollider, but all events thereafter are triggered when I click anywhere.
The game object has no GUIElement on it and uses a PolygonCollider2D on it. I have paused the game after to see if the collider or anything else on the gameobject changes, but nothing happens. I am using C# for all my scripting.
Thank you for your help.
Here is my code
usingUnityEngine;
usingSystem.Collections;
publicclassGoat : MonoBehaviour {
privatestaticSprite[] goats;
privatestaticstring[] goatNames;
voidAwake(){
Application.targetFrameRate = 60;
}
voidOnMouseDown () {
ScoreTracker.incrementScore ();
increaseGoatSize ();
audio.Play();
}
voidOnMouseUp () {
resetGoatSize ();
}
//LoadsallGoatssoIcanchangetheGoatifIeverneedto.
voidStart () {
goats = Resources.LoadAll<Sprite> ("Sprites/Goats");
goatNames = newstring[goats.Length];
for(inti=0; i< goatNames.Length; i++) {
goatNames[i] = goats[i].name;
}
}
voidUpdate () {
}
//ChangestheGoat
privatevoidswitchGoat (stringgoatName) {
SpriteRendererrenderer = GetComponent<SpriteRenderer>();
renderer.sprite = goats[System.Array.IndexOf(goatNames, goatName)];
transform.position = newVector3 (0.18f, 0.5f, 0);
addCollider ();
}
privatevoidincreaseGoatSize () {
transform.localScale = newVector3(1.25f, 1.25f);
}
privatevoidresetGoatSize () {
transform.localScale = newVector3(1f, 1f);
}
//WhenIchangetheGoatIhavetochangethecollideraseachGoatisalittledifferent
privatevoidaddCollider () {
Destroy (renderer.collider2D);
PolygonCollider2Dcollider = (PolygonCollider2D) gameObject.AddComponent<PolygonCollider2D>();
collider.enabled = true;
}
}
Your answer
Follow this Question
Related Questions
How to Address Texture2D Elements from a Sprite with Sprite Mode: Multiple, in Code? 1 Answer
Help With Simple Jump Script 1 Answer
Script won't destroy prefab clones... 1 Answer
Unusual error regarding movement in a top down 2d sidescroller 1 Answer
C# Procedural Tilemap Generation - Each instance of tilemap rendering the same 1 Answer