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 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
Add comment · 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

$$anonymous$$adDave and $$anonymous$$evin 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 $$anonymous$$onoBehaviour. This means that you should be able to use $$anonymous$$onoBehaviour (or its parent class, Component) as the type for "someVariable".

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

Thanks $$anonymous$$evin! Wanted to comment earlier. variable : $$anonymous$$onoBehaviour worked like a charm.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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
Add comment · 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
0

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
Add comment · 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

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

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

GetComponent with variable script possible? 1 Answer

GetComponent() - Is it possible to pass a string variable as name of the script? 3 Answers

How to get variable from another unknown script ? 1 Answer

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

GetComponent via 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