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 cncguy · Jul 05, 2010 at 09:08 AM · speedreferencemonomonobeahviour

References to script objects outside of the gameobject in a class that does not inherit from monobehaviour

I have a number of gameobjects that get created dynamically (via Instantiate) that have a main controlling script on them. After they are created I need to access the transform and the main controlling script frequently from another script on the parent object. In order to avoid using GetComponent all the time I have created a wrapping class for the Instantiated gameObjects. The wrapping class does not inherit from Monobehaviour. It takes in an instance of the GameObject created via Instantiate and saves Public references to the transform and main controlling scripts. This way I can instantiate each object, wrap it in the wrapper class and then pop it into a Javascript Array. I can then iterate through the array and access the transform and script via WrapperClass.trans and WrapperClass.script. The reason for the wrapper class was to allow direct references to the transform and controlling script of the object without using GetComponent.

Now I thought that that would be a fast solution. However I am now having my doubts as it seems that there is some overhead I am not aware of in doing what I am doing. Can anyone shed some light on this?

I guess the simple answer I am looking for is whether what I am doing is a bad idea or not. Should my wrapping class inherit from MonoBehaviour? Am I meddling with non-managed code if I don't inherit from monobehaviour? Or is it slow because the type is being determined at runtime when I access it from the array? Hmmmm should I use C# with generics?

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

2 Replies

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

Answer by StephanK · Jul 05, 2010 at 09:29 AM

C# with generics would definetly be a faster solution. In general anything that is typed will run faster. (Also true for javascript) Normally I keep references to all the transforms and Components that I need frequently in the script that uses them. Just make a Transform[] or List and initialize it in Start() using GetComponent<>().

Comment
Add comment · Show 1 · 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 cncguy · Jul 05, 2010 at 09:27 PM 0
Share

thanks these comments are helpful. For all objects that I know will be around I keep references to things I want to access. In this case these objects are spawned dynamically at runtime in possibly unknown numbers. I don't want arrays with null references in them to account for the max number of possible objects that will be created.

avatar image
1
Best Answer

Answer by Mike 3 · Jul 05, 2010 at 10:46 AM

Even if you do have to get the variables out of the array, as long as you're just doing a simple cast it should still be a heck of a lot faster than accessing the components on the gameobject, so yes, your encapsulation idea is a good one

You don't want to make this class a monobehaviour - it doesn't really make much sense, as you won't be able to create instances of it without using AddComponent on a gameobject

You're not going to see a huge improvement with generics (basically it'll just save you the cast when accessing it), as long as you're grabbing things properly from the array (which won't type it at runtime):

var whatever : YourClass = yourArray[0];

Will be a lot faster than the same thing without the type (which will type it at runtime):

var whatever = yourArray[0];

That's not to say you shouldn't go that route though - generics make the code a lot easier to read, and give you type safety (So you can't push a Halibut instance into a Hippo list for example)

Comment
Add comment · Show 1 · 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 cncguy · Jul 05, 2010 at 09:28 PM 0
Share

Hi this is helpful thanks.

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

Setting up reference to an inactive GameObject through script 1 Answer

Are there any tips for speeding up SQLite through Unity on iOS 1 Answer

Altering Character Motor max speeds from script 0 Answers

Can I get a reference (not a copy) to a string from a script? 2 Answers

Error CS0120 0 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