- Home /
How to give reference to scripts added dynamically?
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?
As you see, I clicked on the script"ChangeCursor" in the project view, and in the inspector I can drag my assets to the public variable
Fascinating - I stand corrected :) guess it only works for dragging then then rather than AddComponrnt...
Answer by Wolfram · Jun 02, 2012 at 12:37 PM
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?
@Wolfram , so as you told me, assigning the texture to the script in the project view must (as the post) must show the texture whenever I attach the script to any object, logically it is like scaling a prefab in the project view effects all the instances and might be overriden if I change the value of a specific instance, but this is not what I get! I need the same texture for all the instances (and this has to be the power of inheritance) I can't figure out why this is not working with me
Hm, I think @whydoidoit found the answer in his comment to your question: It might be that this "default assignment" is an Editor feature, meaning it will only work if you drag the script into your scene, which will then launch an internal Editor script making the default assignments to that instance. It might be that this is not done when instancing the script via AddComponent()!
If that's the case, I guess you have two options: a) ins$$anonymous$$d of just instantiating the script, see if you can ins$$anonymous$$d create a prefab containing the script, and then instantiate this prefab ins$$anonymous$$d of merely the script. But this might not be feasilble, depending on what you're actually doing in your script/with your object. b) use the solution I described, by assigning your textures in the "master" script code that also instantiates your script.
Answer by FormativeTech · Jun 01, 2012 at 02:25 PM
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
Thanks for ur post but what do I do with the component? I need to send a Texture2D as a reference to a public variable found in my script, already I am attaching the script to a gameObject dynamically, if my script is attached to a gameObject already found in the scence, I could simply drag a reference. thx btw (GetComponentInChildren) might help me in other issues
If the script already has the texture attached as a variable in its Prefab, then it should be automatically accessible to the script, whether or not the script is dynamically attached to a GameObject during runtime.
If this isn't working, then you should be able to use GetComponentInChildren to get the Texture2D Object. Component is a class that represents any Object attached to a GameObject, so it should be directly castable to a Texture2D (as Texture2D->Texture inherits from Object). So you could get the component in your Start function for the script, cast it to a Texture2D and assign it to the script variable.
As I said, I haven't tried this out before, but judging from the documentation it should work.
Your answer
Follow this Question
Related Questions
How to drag a GameObject with touch but at a limited speed 1 Answer
Multiple Touch Drag iOS 0 Answers
Unity Touch Controls, Drag and DoubleTap 1 Answer
Drag on Scroll Rect Non Proportional 0 Answers