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 dansav · Feb 27, 2012 at 08:28 PM · stringreferencemonobehaviourmonobehavior

Is there a way to reference a script with a string?

using pragma strict I would like to reference a script before it is attached to anything by using a string that the user passes in.

In other words convert a string into a monoBehavior reference?

Is there a way to do that?

Thanks,

Comment
Add comment · Show 1
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 WillTAtl · Feb 27, 2012 at 09:06 PM 0
Share

you should mention you need to do this in strict mode. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by CHPedersen · Feb 27, 2012 at 08:41 PM

There is. You can try to call the version of GetComponent that takes a string argument:

http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html

If you don't know which gameobject the required script is attached to either, you'll have to get a list of all gameobjects using this method:

http://unity3d.com/support/documentation/ScriptReference/Object.FindObjectsOfType.html?from=GameObject

Passing in GameObject for the type you're searching for, and then call GetComponent for the required script on each of them one by one.

Like so, it is technically possible, but please note that I don't recommend doing it. It's just too errorprone, and makes for inelegant code, besides. One should always use the generic version of GetComponent instead of the string version, where possible, and besides, searching for a script this way is not likely to be efficient, in case you need to do it on a frame-by-frame basis.

Comment
Add comment · Show 4 · 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 dansav · Feb 27, 2012 at 08:59 PM 0
Share

Sorry I wasn't too clear. I meant access the script using a string before it's attached using AddComponent. Is that possible.

avatar image kevork · Feb 27, 2012 at 10:03 PM 0
Share

What are you accessing if the script isn't attached to the object?

avatar image WillTAtl · Feb 27, 2012 at 10:08 PM 0
Share

his previous question gives some context, this one is follow-up... http://answers.unity3d.com/questions/221685/pragma-strict-problem-with-getcomponent-and-script.html

avatar image dansav · Feb 27, 2012 at 10:09 PM 0
Share

WillTAtl: Thanks for adding that link to make things clearer.

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

7 People are following this question.

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

Related Questions

Assign a reference to a Monobehaviour 1 Answer

Accessing a script only using its string 3 Answers

How to reference an EditorWindow script in a MonoBehaviour script OR how to pass variables from a EditorWindow to a MonoBehaviour script? 1 Answer

Object reference not set... but... it is? 0 Answers

Grabbing a value using a string in the reference 1 Answer


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