Hello everyone,
I have a script that is being added dynamically to a gameObject.
I have a Texture2D that I attached to the script directly(dragged ref) in the inspector, since there is no gameOjebt that the script is attached to. but the texture won’t appear,
I tried the same script when attached to an empty game object found in the scene, and added my Texture2D, it works fine!
I thought of Texture2D.FindObjectsOfTypeIncludingAssets(), but I think there must be a better way for this,
Is there a supplementary way to give references for my assets if they are attached dynamically?
The snapshot you posted is the way to do it if it’s always the same object you want to have assigned.
Otherwise, the most convenient way to do it is, create a public var for every asset you want to assign in the script that instantiates your object. Then, directly after the line performing the instantiation, you can assign these values to the scripts of your newly created instance.
With this approach, you can now regularly assign all required assets in the Inspector of the calling/instancing script.
EDIT: Whoops, misinterpreted your question as your title has little to do with your actual problem, which seems to be that you can’t see a texture although you assigned it as seen in your snapshot. Well, both approaches (mine and yours) should work, so the problem seems to lie somewhere else. When you enter Play mode and the object is instantiated, does it have the textures that you dragged onto the original script correctly assigned?
I haven’t personally come across this before, but GameObject.GetComponentInChildren(type : Type) might be what you’re looking for. It returns the first component of the specified type for a GameObject (and all of its children). There’s a also a version of the function that can return multiple components for a given type.
Have a look at the Script Reference for more details.
http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponentInChildren.html
http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponentsInChildren.html