- Home /
What is an instance of a GameObject?
Does dropping a GameObject into the Hierarchy (scene) or dropping a GameObject into a public variable in a script attached to a GameObject in the Hierarchy create an instance of that GameObject?
And does that also make instances of all the components (especially script class files derived from MonoBehaviour) on that GameObject?
Thank you Steve. That's what I thought. I just wanted to be sure.
So it means that when I call a method in a script component of an instantiated GameObject the called method doesn't have to be a static method. (I know when you call a method on a Class (no instance) either the method has to be static or else you have to create an instance of that Class, e.g.
ClassName a$$anonymous$$ethod = new ClassName;
a$$anonymous$$ethod.methodName();
If a script is attached to a game object or prefab and you do an Instantiate() on that game object, you get a new copy of the game object including all the scripts attached to that game object.
Answer by Slobdell · Jun 09, 2013 at 01:16 AM
If you drag the object into the scene, it will have an instance created upon running your app. If you place a gameobject in a script, it will be instantiated if you instantiate it...
GameOjbect newObject = new GameObject();
When you instantiate a gameobject by placing it visually, or programatically, all attached scripts will also be instantiated with it. If you have a prefab with a script attached say, they essentially become one object. Can't instantiate one without the other unless you write some code to detach and destroy the script.
Steve
Dropping an object into a public variable only assigns the object or prefab reference to it. If it's a prefab, you must instantiate a clone in order to have a real scene object - the prefab itself is a kind of "game object template", not an actual scene object.
I know this is going to be stupid, but here goes...
What if you place an instantiated GameObject(2) (from scene or hierarchy) into a script that is on an already instantiated GameObject(1). When you run the app will GameObject(2) be accessible as an instance or just as an uninstantiated Class?