- Home /
How to tell if something ISN'T clicked on?
So I'm working on an RTS, and I have a JS script that highlights any unit I click on. It works great, I can immediately see stats, controls, etc. for whichever unit I pick. The only problem is, when I want to pick a different unit, the first unit I clicked on remains highlighted.
I'm solid enough with my JavaScript (and with searching unityAnswers) to know if I at least had an idea of how to do it, the code would come in time, but I'm basically at a loss for where to start.
I considered using one tag for units and one for the ground, so when you click the ground the unit un-highlights, but then if I switch from one unit immediately to another, the first one would remain highlighted.
If it helps, my highlight system is based on a line renderer that is added via script when the unit's collider is clicked on. I simply need a way to remove the line renderer when the player clicks on anything that isn't that specific unit's collider. Any thoughts? Hopefully the solution is simple, and I've simply way overthought this :)
Answer by Jamora · Jul 22, 2013 at 11:04 PM
I would approach this by creating an interface IClickable for each object that can be clicked and keep a List or an Array in a manager somewhere. Whenever anything IClickable is clicked, it's added to that list and a line renderer is added. and whenever a highlighting removal operation (in this case, another click) is performed, it is removed from the list, alongside all the required operations, such as removing the line renderer.
Because it seems you only need one highlighted unit at any given time, a simple variable might be sufficient, but this construct supports mulitiple highlights aswell.
I'll keep those ideas in $$anonymous$$d. I haven't heard of the IClickable interface, is that your own term Jamora? I agree with you guys, an array to keep track of which unit is selected at one time seems like the most logical method. And yeah, if I decide to implement a multi-select sort of function later, it would still work. Arrays, unfortunately, are something I'm not very well-practiced with, but it won't be the first time I've done a little online research. I'll give it a try and let you know how it works out.
If you aren't familiar with the term interface, you should look into it at some point. The C# Program$$anonymous$$g Guide gives an explanation on how to use them, they're pretty much the same in UnityScript, except ins$$anonymous$$d of : you use implements
Found this in my research, if anyone else finds themselves trying to solve a similar issue, this guide explains it pretty well.
http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?
Thanks for your help, guys.
Answer by fransh · Jul 22, 2013 at 11:12 PM
A really simple way would be : Clear the entire selection list, or what ever you use to keep track of the current selection, when ever a input from the selection key is registered.
If the player happened to click on the same unit again (so it should not leave the selection at all in this exceptional case), it will simply be re-selected again because that function that caused it to be selected in the first time, is called again and the player never notices a thing in that situation.
Your answer
Follow this Question
Related Questions
Unity Editor style selection in game 1 Answer
Virtual Reality collider help 0 Answers
Internal collisions 1 Answer
Box Selection 0 Answers