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?
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.
Your answer
Follow this Question
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