I am having maddening difficulties in Unity trying to handle click events on colliders and at this point my code is a mess and I am pretty much back to where I was when I started having the problem over a week ago.
I think most of the issue is that I am not handling the fact that Unity GUI button clicks and collider clicks happen repeatedly as the mouse button is down.
Basically, I have a grid of primitive cubes with terrain textures on them. As these cubes are clicked while in TerrainEdit mode, the currently selected terrain texture is loaded onto the cube. This part works perfectly. If a cube is clicked while in TokenEdit mode and it does not already have a Token prefab sitting atop it (the token prefab consists of a simple imported, circular mesh with a texture of a character on it), then a new Token prefab is instantiated and the currently selected Token texture is loaded onto it. Finally, if there is already a Token on the Terrain tile, then the collider of the Token gets the click and causes the Token to be destroyed.
All of this is actually working (well, it was until I tried to add the collider.enabled assignments to a constructor of the Token class and now I am getting an exception), but it is not smooth at all. In its current state, it is difficult to add the tokens and remove the tokens because of the click events happening multiple times. I have tried to create logic using timers that prevent new tokens from being destroyed/added if one has just been destroyed/added within the last second. I was hoping this would protect against both double-clicks in addition to the events firing multiple times in Unity.
So, I obviously need more training for scripting in Unity. If someone could perhaps point me to a good tutorial or reference on this matter that would be great.
Additionally, if you would like to take a look at my code I intend to place a link in this post where it can be downloaded in a bit. While my code is most likely not very good, it is heavily commented so I don’t think you would have too much trouble following the logic.
I really appreciate any help at all for this as it has driven me crazy!
Links to code: