Hi I’m faily new to Unity although I already love it!
I have been building a selection script for use in a basic RTS game.
My question is:
Is there a way where I can get my main selection script to call functions on the scripts contained on my selectable GameObjects?
an example:
Selection Script → Detects mouse click → casts ray → script detects that gameObject is selectable → ?? → gameObject flags that it’s selected
How do I then call the relevant functions within the selected gameObject’s selectable script?
I’ve looked at BroadcastMessage but didn’t want to have each object check to see if it was the one that was selected within the recieving code.
Any help/advice? Should I just bite the bullet and use BroadcastMessage?
When you use broadcast message, you broadcast it to a single object.
Assuming the raycast hits an object named “Zergling15”, you would do:
Zergling15.BroadcastMessage ("FlagSelection");
Even though its called “Broadcast” it doesn’t actually call the function on multiple objects. It only calls it on that object, and on the children of that object. Alternatively you can also use SendMessage if you don’t want the children of that object to be checked.
So presuming my Raycast hits and stores the RaycastHit in a hit variable I could call it like so:
hit.gameObject.BroadcastMessage("functionName", true);
and the message could be recieved in any script within the selected gameObject?
I’m asking because I’m away from my computer or I would test it myself.
and Thanks for the help
So presuming my Raycast hits and stores the RaycastHit in a hit variable I could call it like so:
hit.gameObject.BroadcastMessage("functionName", true);
and the message could be recieved in any script within the selected gameObject?
I’m asking because I’m away from my computer or I would test it myself.
and Thanks for the help
It would actually be:
hit.transform.gameObject.BroadcastMessage("functionName", true);
Raycast hit does not store the gameObject, just the transform. But you can access the gameObject through the transform.
No problem. 8)