Hi, I am new in Unity and I am creating gameobjects dynamically during runtime. I want to attach a function with parameters to these gameobjects so that the function is executed when it’s clicked. I can write onMouseHover function, put it in another script and have addComponent to attach it to the gameobject. But I am not sure how to pass parameter that way.
For example:
when the game is started, 10 game objects are created. Each of them has property “name” (a string). I want it so that when the object is clicked, it will show it’s name. But I need to pass the parameter “name” of the object to the onMouseHover function. I can’t figure out how to do this.
Thanks
If you write a script that you add to each of these 10 objects, you could make a function that returns the name of the object it is applied to. For instance, gameObject.name gives you the name of whichever object you are currently interacting with. You could pass this info with a literal “return” command in your own function, or you could code it to directly interact with another object, such as changing a bit of text. (Using a GameObject.find to search for the object you’d like to change, and a getComponent() to look for components to adjust.)
Depending on your needs, you may be able to use some of Unity’s built in functionality to simplify some of your coding by applying an EventTrigger component onto each of the 10 objects, along with the script. This could allow you to call any functions found in your script whenever you do certain actions (clicking, hovering, etc).
I hope this helps.