• 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
Question by bkovner100 · Aug 23, 2012 at 12:51 PM · variablegetcomponentstringtypeparameter

Passing a Script Name to a Function

Is this possible? I'd like to be able to input the script name as a parameter in the function like so:

 function CacheScript(someVariable : scriptName[], anotherVariable : scriptName)
 {
    for (var i : int = 0; i < gameObject.Length; i++)
    {
       someVariable[i] = gameObject[i].GetComponent(anotherVariable);
    }
 }

Problem is figuring out which type to declare for "someVariable" and "anotherVariable" since I won't know the variable type until I enter the custom script name when I call the function. I've tried researching all the different types I could use to declare the variable type but coming up empty. Any help would be hugely appreciated!

Comment

People who like this

0 Show 3
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 bkovner100 · Aug 25, 2012 at 05:09 AM 0
Share

MadDave and Kevin Harris, thanks for the suggestions. I apologize - I realized that my question wasn't specific enough. "someVariable" array has to be one of many different types of script I'm going to call. So I won't know the type (name of the script) until I run the function.

Since the "someVariable" type could be one of many different scripts, how do I declare the type? Is this possible because the type would have to encompass all of the different scripts I intend to call.

avatar image Kevin Harris · Aug 27, 2012 at 04:17 PM 1
Share

It looks like you're using JavaScript. If so, then by default all of your scripts should inherit from MonoBehaviour. This means that you should be able to use MonoBehaviour (or its parent class, Component) as the type for "someVariable".

avatar image bkovner100 · Dec 18, 2012 at 08:10 AM 0
Share

Thanks Kevin! Wanted to comment earlier. variable : MonoBehaviour worked like a charm.

2 Replies

  • Sort: 
avatar image

Answer by MadDave · Aug 23, 2012 at 03:23 PM

You should be able to use UnityEngine.Object (or just Object) instead of String.

Comment

People who like this

0 Show 0 · 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

Answer by Kevin Harris · Aug 23, 2012 at 03:55 PM

Have you tried making someVariable an array of Components instead of Strings?

You might also consider GameObject.GetComponentsInChildren if the target game objects are children of the object running the query.

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

How to get variable from another unknown script ? 1 Answer

GetComponent via String name? 3 Answers

How would I make the GetComponent field a variable? 1 Answer

GetComponent and converting string to Type type 1 Answer

GetComponent from string name? 3 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