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
3
Question by dissidently · Mar 30, 2011 at 10:35 AM · componentobjectsgenericssyntaxaccessing

differences: generic GetComponent.

Not that I know what "generics" means in this construct, but can anyone nicely, gently, kindly and clearly explain the syntax sugar of calling/accessing/getting non attached objects, attached objects and their components in Unity? In UnityScript please, first, then maybe C#

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 AngryOldMan · Mar 30, 2011 at 10:45 AM 0
Share

could you please rename you question title to something like "Accessing Components of NPC GameObjects" or something like that which is more descriptive towards your question. What the hell does "if .() is generics What is (script1)?" mean anyway?It looks like homemade broken code.

avatar image Mike 3 · Mar 30, 2011 at 11:27 AM 0
Share

Changed the title to make more sense to anyone reading it later

3 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by Mike 3 · Mar 30, 2011 at 11:02 AM

Note - the use of the word Script below refers to your own script. Substitute in YourScriptName for Script there

GetComponent.<Script>()

finds the component of type Script, and returns a Script reference to you

GetComponent(Script)

finds the component of type Script, then returns a Component reference to you

GetComponent("Script")

finds the component with name Script, then returns a Component reference to you

The difference between name and type is a large one - errors with type will throw compile time errors, while errors in the name will throw runtime errors

The difference between it returning a Script and a Component reference to your script is that a Script reference will immediately have all of the Script functions and variables available to you, whereas with Component, it'll either need casting manually, or it'll need to dynamically call functions and variables, which again will give you runtime errors

Regarding c#:

Since c# doesn't do the whole dynamic typing thing, you have to manually cast the latter two versions, so your code looks like this for all three:

Script script = GetComponent<Script>();

Script script = GetComponent(typeof(Script)) as Script;

Script script = GetComponent("Script") as Script;

I would recommend always using the generic version whenever possible in both languages - it's the most robust version, giving you the correct compile time errors you need to solve issues

Comment
Add comment · Show 9 · 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 AngryOldMan · Mar 30, 2011 at 11:19 AM 0
Share

"The difference between name and type is a large one - errors with type will throw compile time errors, while errors in the name will throw runtime errors" this is incredibly useful thank you! The whole answer is to be honest, vote up!

avatar image dissidently · Mar 30, 2011 at 12:14 PM 0
Share

so the correct use, to return a script to a variable from within a script on the same object would be: myVarHoldingScript = GetComponent.("nameOfScript") ?

avatar image Mike 3 · Mar 30, 2011 at 12:48 PM 0
Share

no - myVarHoldingScript = GetComponent.();

avatar image dissidently · Mar 30, 2011 at 02:24 PM 0
Share

ok, so why is it not myVarHoldingScript = GetComponent.(); ?

avatar image Mike 3 · Mar 30, 2011 at 02:57 PM 0
Share

Javascript files make an implicit class called NameOfScript behind the scenes, and put your code into that. Generic stuff always expects a class name, not a filename

Show more comments
avatar image
7

Answer by Statement · Mar 30, 2011 at 06:44 PM

Generics can be thought of as variables, but they express a variable type, not a variable value, and they are set compile time (static typing) rather than run time (dynamic typing).

It allow you to change type of something in the context. The generic GetComponent looks like:

public T GetComponent<T>() where T : Component
{
    // It gets the type of T and calls the 
    // non-generic variant, for convenience.
    // Then it cast the result to T.
    return (T)GetComponent(typeof(T)); 
}

The code where T : Component is a constraint on the type T.

It says T must be a type that is a subclass of Component, so you can't call for example GetComponent<int>();, because int isn't a subclass of Component.

It is quite possible to call GetComponent(typeof(int)); but it is considered an error, and the compiler won't whine about this since it can't apply the constraint in the same manner as with the generics. For this reason the generic version provides stronger type safety (less bugs).

Consider calling GetComponent<BoxCollider>();. We could just swap out T for BoxCollider to see what the generic version would resolve to:

public BoxCollider GetComponent<BoxCollider>()
{
    return (BoxCollider)GetComponent(typeof(BoxCollider)); 
}
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
-1

Answer by AngryOldMan · Mar 30, 2011 at 10:42 AM

you could set the object as a prefab in a variable. this is the way we do it for absolutly everything. There is possibly a better way but I feel like that with all scripting.

var prefab : GameObject;

function Update () { if (prefab == null) { prefab = GameObject.FindWithTag("TaggedObject"); } } function DoSomething() { DoThis = prefab.GetComponent(Script) DoThis.Variable = whateveryouwant; }

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 dissidently · Mar 30, 2011 at 11:04 AM 0
Share

I'm more confused.

avatar image AngryOldMan · Mar 30, 2011 at 11:18 AM 1
Share

well your comment doesn't help inform me as to what confuses you so why the hell should I explain anything, your question is confusing but I still did the best to provide you with an answer have some damn courtesy and be more informative.

avatar image Uzquiano · Mar 30, 2011 at 12:21 PM 1
Share

Bob I have a question about your code. So what you are doing is accessing Variable from a prefab loaded in the inspector...

What is the purpose of your code? I mean I understand your lines, but a practical use wuld let me understand it perfectly.

Thanx in advance

avatar image AngryOldMan · Mar 30, 2011 at 01:53 PM 0
Share

never said load the object in the inspector i just answered with $$anonymous$$mal code as it wasn't an indepth question.

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

No one has followed this question yet.

Related Questions

Adding Materials to an object. 1 Answer

What is the syntax for adding some generic type of component without an object reference? 1 Answer

Syntax for method wich returns generic type (own class)? 0 Answers

What is the basic syntax of accessing components? 1 Answer

How to Copy a Component if it contains List? 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