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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by superventure · May 26, 2013 at 04:56 AM · variablegetcomponentaccessing

Can't understand generic getComponent for Js

What's the deal? I looked over the other posts regarding the generic versions of getcomps but I still don't understand exactly how to use it. I'm using js.

I am curious because I read somewhere that generic is better for performance? If so, how so?

I am practicing with this but I keep getting errors

 var script : behaviorScript;
 script = GetComponent.<behaviorScript>(); 
 headBone = script.headBn;



Error: behaviorScript is inaccessible due to its protection level.

what's wrong with it?

My goal is to not have to type out 'GetComponent' every time I need something from another script. I would like to be able to just say 'script.Blah()'. I use getcomponent quite a bit and in update, so if I save on performance I really want to learn this.

Thanks

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 superventure · May 26, 2013 at 05:00 AM 0
Share

Ps Does

var script : someScript;

actually detect any script named someScript that is attached to this game object? Even if the script asking for it is a child of the intial gameobject?

avatar image numberkruncher · May 26, 2013 at 01:48 PM 1
Share

Generics are typically more efficient than types since they often avoid the need to use reflection. Whether or not generics are more efficient for the GetComponent function is a different question since we do not know how GetComponent is implemented. I shouldn't think that there would be a noticeable difference in this case.

Though, the generic version of AddComponent is likely to outperform the non-generic version since it will almost certainly avoid the use of reflection. But again, this depends upon how it is implemented.

I would recommend to use the generic version for consistency throughout your scripts.

A little tip with regards to your na$$anonymous$$g of classes and other types. I would recommend using ClassNameLikeThis ins$$anonymous$$d of classNameLikeThis because this will help to make your code more readable by avoiding confusion with variables and fields.

1 Reply

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

Answer by superventure · May 26, 2013 at 06:49 AM

I found out what the problem was- I had the variable I wanted to access marked as private. When I declared it public, it worked. I would still like to know if this method is better for the engine, if someone could please explain?

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

14 People are following this question.

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

Related Questions

Help with accessing scoring system variable C# plz. 3 Answers

Accessing other gameobject's script variables : why doesn't this work? 2 Answers

Changing variables in another script 3 Answers

How to set a variable equal to another variable in another gameobject? 2 Answers

Why can't I change another script's variable with this script? Thanks, 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