- Home /
Is this a correct (efficient) way of selecting and handling units?
Essentially, what I'm trying to do is a selection of objects (units) for an RTS game.
I've thought of the many ways to do this. Currently, the way I'm doing it is I have an empty game object named SelectedObjects. Whenever a unit with a tag "selectable" is picked, he is dynamically parented to the empty game object, and then whenever the player does an action, the message is sent to all the children of this object.
I wonder if the dynamic parenting could pose a problem? Currently I'm avoiding possible ones by parenting only objects with no parents, but I'm new to the engine, and I'm not sure if it will cause bugs or screw up something later. Also I wonder how efficient it is to loop through all the children (although they will probably be less than 100 at most). That and I'm using up the objects tag that might be used for something more important.
Another way I've thought of handling this issue is by making a Selectable component, which will have a boolean "selected" variable. But I wonder if this will cause any performance problem since every time the player clicks the mouse to order the units to move, I will have to call the Object.FindObjectsOfType . The docs say it's slow and not call it every frame, but considering an average of.. let's say 5 clicks per seconds, I'm not sure if it would cause a problem.
Or maybe if the dynamic parenting doesn't cause a problem, I can use it to loop through the small array of saved objects, along with the "Selectable" component to check if I can add an object that was clicked on to the selected objects?
Any suggestions? I'm really lost!
Your answer
![](https://koobas.hobune.stream/wayback/20220613082653im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I change component properties without overriding them with parent component? 1 Answer
MouseButtonDown stops working after removing an component and raycast missing an target 0 Answers
2D Animation does not start 1 Answer
Animator component, instead of Animation 2 Answers
Using GetComponent in multiple scripts for same component? C# 2 Answers