Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 losingisfun · Nov 09, 2016 at 03:11 AM · componentscache

C# - can I cache a GameObject script component in another script?

I'm making some AI and I'm using this line a lot:

GetComponentInParent();

I know it's bad practice t call this too often, and I'd like to cache it if possible. There is a good chance if there are multiple game objects interacting at once, it may get called a dozen times.

Normally I cache components, however, I'm not sure how to do this with a script? or if it's even possible? I've tried this: Component parentScript = GetComponentInParent(); however, later on, when I try to get public variables like: parentScript.health, it doesn't pick it up.

Does anyone know if it's possible to cache a script component? (Or at the very least, link up variables some how?)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · Nov 09, 2016 at 03:30 AM

You can get the component with the generic version, if you return a type component it will only have the Component type methods and properties/fields available. So.

 //... snip
 private AIGameObjectScript aigo; // cached version in script, just a field
 
 // Start or where ever makes sense.
 void SomeMethod()
 {  
     aigo = GetComponentInParent<AIGameObjectScript>(); // The generic call that get the first AIGameObjectScript type component from the parent.
 }

To repeat myself, getting the component type doesn't give the type you need(unless it does, but you stated it doesn't) You could cast it but you might miss the target.

Comment
Add comment · Show 3 · 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 losingisfun · Nov 09, 2016 at 04:34 AM 0
Share

well this doesn't entirely solve my problem. - But maybe you could suggest an alternative. Is there a way to store a reference to a variable, as a variable, within another script? for example, i've passed arguments with: void Something(string ref argument) can i store a variable as a reference? like: 'private ref bool something' and then if I set 'something' it will change the referenced variable as well? or is this just a stupid question?

avatar image losingisfun · Nov 09, 2016 at 04:35 AM 0
Share

to put it short, I just need some way to cache references to variables in another script so my code isn't too clunky

avatar image Landern losingisfun · Nov 09, 2016 at 05:13 AM 1
Share

Yes, that is the point of reference types(classes), unless you make a deep copy of the type it will be a reference, that is to say if you have a master/manager script keep references(pointers) to objects in other scripts with few exceptions anything you do in the master/manager script will affect the referenced objects, generally speaking you don't need to use the ref keyword either. As an example years ago i made a breakout clone, when each brick was instantiated in the level i had it register itself(read Add) it's own Brick instance to a $$anonymous$$anager script and a List, this allowed me to do various management on the stack or by references(IndexOf on the generic List) and left a lot of management to the $$anonymous$$anager singleton script. Like $$anonymous$$anager.DestroyBrick(this) from the OnCollision invocation on the Brick object and do various things to the score all in the manager script.

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

57 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 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 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 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 avatar image

Related Questions

How to prepopulate Font Cache? 4 Answers

catching a component 0 Answers

Question about Unity documentation GameObject.Find 2 Answers

Unauthorized use of Caching API in webplayer 0 Answers

Cannot download large assetbundle with WWW.LoadFromCacheOrDownload on old iOS device 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