Assigning Instantiated image to gameobject in script
Hi. I've tried a lot and nothing works.
Gameobject.Find()
Instantiating the Image and assigning it to the gameobject
Instantiating the Image as a Gameobject and assigning it to the gameobject.
Nothing works.
I instantiated a Gameobject called UI (in the scene it's called UI(Clone). One of Its children is a UI image called ImageWhiteDot. I Wish to assign that specific UI Image to a Gameobject declared in one of my scripts, but using Gameobject.find or findbytag, it Just doesn't seem to find it. If I look at the script in the inspector window, I want to see "ImageWhiteDot" attached/assigned to my Gameobject.
Any help would be appreciated. Thanks
Answer by inieuwoudt1 · Sep 02, 2017 at 07:38 PM
@MaxGuernseyIII Thanks. I've just found the solution. For anyone having this issue in the future:
Create Gameobject
Drag everything that you want to instantiate (in my case one prefab for multiplayer spawn) into the gameobject.
Everything like the UI elements etc. can be added into scripts' variables.
Drag the Gameobject you created into the resource folder and use a script to Load that Prefab
GameObject yourgameobject = (GameObject)Instantiate(Resources.Load("yourgameobject"));
Answer by MaxGuernseyIII · Sep 02, 2017 at 05:14 PM
I would put a script on the prefab with a field for the image and edit the prefab to attach the image to the script's property on that prefab. Then you can use GetComponentInChildren() on your instantiated prefab to get the configured instance of your behavior.
Once you have a reference to your custom behavior, you can just read the image off the field that stores it and do whatever you want with it.
Your answer
Follow this Question
Related Questions
UNET Clients Laggy 0 Answers
How to limit this particular raycast distance? 3 Answers
SQL Unity Android 0 Answers
Unexpected Symbol 'break' 2 Answers
Script attached to An enemy but dosent work right if there are multiple enemies. 2 Answers