On click, select game object -working with objects in scene but not instantiated objects

Hey everyone! I hope I can get some help with this issue, because it is BOGGLING my mind. I have created an RTS (starcraft-esque) game for Newgrounds’ Pixel Day 2022 (it is called BitBase if you want to try it out and get a better idea of the issues I’m having lol, but this post is about one issue in particular).

  • **Main Issue: ** When I run the game in Unity, I have 5 ‘peons’ in the scene, along with a few resources for them to gather and a central node, and I am able to left-click and ‘select’ those units without any issue, but new units spawned in WILL NOT activate their selected function this way. The way the game functions right now, you can drag and select units, which works for all as I have a script on a boxCollider and lineRenderer that does all of my detection for that. Also functioning is a UI script that pops an info panel when a unit is left-clicked (not click and dragged)…

  • Troubleshooting steps I have taken;

  1. I have broken down the units that are in the scene bit by bit and they are NO different than the units being instantiated (they are just prefabs that I dragged into the scene, but the same prefab being instantiated).
  2. Changed my Ui script to access the clicked unit script (using PhysicsRaycast2D then getting the hit collider’s Unit component and tried to FORCE the selector active that way… to no avail)
  3. Tried an OnClick function on the Unit monobehaviour script
  4. There are no colliders blocking the unit, the units have rigidbodies, they are on the proper detection layer and it IS detecting the unit being clicked, because it is popping the UI info, but somehow getting lost in actually accessing the Unit… :S

There is probably something super simple that I’m missing or a conflict in the UI scripts possibly, but I’m at my wits end here. Also, I am self taught, so I apologize if any of my terminology is improper; if there is any confusion please let me know and I will try to provide more info or screenshots!

Hi @snoboxstudio,

Maybe somewhere you are checking for name of selected GameObjects… and new Instantiated ‘peons’ have ‘(clone)’ added?

Did your check new Instantiated ‘peons’ during pause in gameplay did all of they [Serializefield] are still atached? Maybe some of components weren’t assigned after Instantiate?

Hope will help, let me know.

To resolve this thread; it appears to be some faulty logic on my part (shocker lol). I broke down my scripts bit by bit and found a redundant ! that was probably throwing things off. After reorganizing and simplifying some conditions, the function now reliably works! But it was strange that it was working, at all, if the logic was flawed.

@privatecontractor thanks again for trying to help!