How do i start a function in a script on an instanced object?

Hey Guys,

I have a quick question:

In my Unity Application i create several objects while running (instancing from one object). All these objects have the same script on them.

Now when the user clicks on one of those instanced objects a Raycast is send to determine if and wich object the user hit. Now i want to start a specific function or set a bool variable for example, but only on that one instanced object i just hit. Is that possible?

EDIT: Another possibility that would be even better is if i could have that script deactivated at the start and then activate it at the moment i hit the object with the Raycast. Is that possible?

I have the RaycastHit object and through that (RaycastHit.transform.gameObject) the instanced Gameobject i hit. I hope u understand what i am trying to achieve!

thanks in advance.


All you need to do is finding your gameObject, in your case with a raycast (you seem to know how). Get the script use GetComponent on this gameObject to get your script. Then you can simply use yourscript.enabled = true to activate it. Or yourscript.AwesomeFunction() to call your function.

Any nonstatic variable exists on every instance independently. And they don’t know what is happening with the other instances by default.