Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by moghes · Jun 01, 2012 at 01:45 PM · referencedragattackdynamically

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moghes · Jun 02, 2012 at 12:26 PM 0
Share

alt text

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

drag reference to script in project.jpg (131.1 kB)
avatar image whydoidoit · Jun 02, 2012 at 12:46 PM 0
Share

Fascinating - I stand corrected :) guess it only works for dragging then then rather than AddComponrnt...

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moghes · Jun 03, 2012 at 01:34 AM 0
Share

@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

avatar image Wolfram · Jun 03, 2012 at 10:41 AM 0
Share

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.

avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moghes · Jun 01, 2012 at 09:37 PM 0
Share

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

avatar image FormativeTech · Jun 02, 2012 at 03:44 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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

How do you make a button trigger an OnBeginDrag? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges