- Home /
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;
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).
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)
Tried an OnClick function on the Unit monobehaviour script
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!
Answer by snoboxstudio · Jan 27 at 01:35 AM
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!
Answer by privatecontractor · Jan 24 at 08:08 PM
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.
Thank you so much for the answer @privatecontractor ! I am checking for the tag "Unit", not the name, that is definitely the first thing I changed xD there was one odd thing where a rally point was being passed to the newly produced unit and then not beco$$anonymous$$g null, which I was able to fix thanks to this comment -so there's something! haha
Other than that, everything is being assigned and attached properly, everything executes 100% except the click to select on the new units O.o I went and checked out my Instantiation function too, and I'm not changing/effecting the prefab's state in a way that should break this... Just to make sure, I checked that there were no overrides on the prefabs existing in the scene, and dragged new prefabs in which ALSO work fine with the click function, but still not the instantiated objects! What the heck am I missing!? D:
@snoboxstudio, maybe when you drag- drop prefab directly to scene it's going to main root in hierarchy.... and new Instantiated prefabs going to some parent which affecting them?
Apologies in advance for this novel; I was thinking that the hierarchy had something to do with it, but when I placed the (working) prefabs in the scene I put some within the parent object they're Instantiated into and some outside of it in the main hierarchy, they were able to be clicked and selected! But the newly Instantiated objects are still unclickable (not.. unclickable.. but 'unselectable' when left-clicking on them).
And just to clarify a bit more, when newly instantiated units are left-clicked a pop up menu references their script and is able to get the specific unit name and information(health, shield, cost, etc). But even when I moved the Unit's 'Selected' function to be called right there in the UI manager, on that same pop up function, it won't do it! D:
I might as well answer your second comment in this one too lol apologies its taking me a while to double check these things and then write out my response.. in addition to that, my power has been going on and off all day -_-
I am not sure what you mean by 'using an event for the click-drag inside' - do you mean a UI event? Not a function within the C# scripts? If so, then no xD And just to make sure we're on the same page in this regard too, the click-drag is working; while mouse is down I have a script that creates a box with a line rendered and adds a collider component.
But the OnMouseDown function isn't triggering a specific function on the Unit's script to set a bool true and setActive an indicator.. it seems so simple and that it's working on pre-instantiated objects means that the function itself is valid but something is getting lost in the call... I feel like my brain is dancing around the answer, grrn...
Your answer
Follow this Question
Related Questions
How to select the trigger layer on a Box Collider 2D 2 Answers
Disable click action when collision 1 Answer
Scaling colliders up and down over time 1 Answer
Velocity doesnt change properly 1 Answer