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 /
  • Help Room /
avatar image
0
Question by zix527 · Jul 06, 2019 at 01:46 PM · script.memory usageallocation

How are scripts allocated,How script is allocated when attached to object

I mean if you attach one script to many objects then there are many instances of script so every object have it's own copy of script or there is one instance of script and every object have it's own set of variables that are in this script?,I mean when i attach the same script to many objects, then script is allocated as many times as necessary or there will be only one copy of script and only variables will be allocated for each obejct?

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
1

Answer by Bunny83 · Jul 07, 2019 at 10:15 AM

When you attach a MonoBehaviour script to multiple objects at once in the inspector, each object will have its own instance of that class. There is no difference between selecting each object one-by-one and adding the script one-by-one or adding them all at once. Components in general are bound to exactly one gameobject.


Maybe you are a bit confused what an instance actually is. A class describes what an object / instance of that class is made of. Though in general object instances do not contain code, only data. The code for a certain class only exists once in memory. The memory an object occupies essentially consists of two things:

  • The type handle of the class

  • All instance fields of the class.


Instance methods of a class have an implicit first parameter (the "this" parameter). All code is actually static code. So when you have two instances of a class each has it's own memory space where the fields of that object are stored. When you call an instance method of a class you always call the same code which only exists once in memory. If it's an ordinary instance method (not virtual or abstract) the compiler will actually directly call that method and pass the "this" reference and all the parameters you have passed as well.


For example a method like this:

 public class MyClass : MonoBehaviour
 {
     public void MyMethod1(string aText)
     {
     }
 }

is actually implemented like this:

 public class MyClass : MonoBehaviour
 {
     public static void MyMethod1(MyClass this, string aText)
     {
     }
 }


So when you call that method on an instance like this:

 MyClass someInstance;
 
 someInstance.MyMethod1("FooBar");

the compiler actually does this internally:

 MyClass someInstance;
 
 MyClass.MyMethod1(someInstance, "FooBar");

The behaviour is a bit different for virtual or abstract methods. Since the actual method might be different for a certain class instance, the call is done through a virtual dispatch. Since every object in memory has a type handle which points to the memory location that describes the type itself, instead of directly calling the method the calling code will lookup the actual method that should be called in the method table of the class. That's why virtual dispatch has more overhead and is a bit slower.


This archived version of an MSDN article describes most of the internal layout. Note the links in the index table try to link to the original page which currently links to an archive.org cached version of the original page


TL/DR: Object instances consist only of a small amount of metadata (type handle) and the actual instance fields. Any code (methods, properties, constructors, field initializer, ...) in a class is only defined once in memory and shared between all instances. This is how almost all compiled languages work.

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

193 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 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 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 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 do i change object localScale axis x but only to the right side ? 0 Answers

My MouseLoook won 0 Answers

Make camera collide 2 Answers

Accessing values on a gameobject 1 Answer

system time image effects 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