- Home /
GameObject Class understanding
Hello , just understanding the fundamentals of Unity I cant understand the GameObject variables in scripts. where are they actually ? are they in hierarchy when we declare them ?
look at this example :
GameObject testObject;
Light testLight;
Void Start() {
testLight = GetComponent<Light>();
//or
testLight = testObject.GetComponent<Light>();
What is different when use the testObject and when we dont use it? what does it actually do ?
Answer by Cherno · Feb 07, 2018 at 07:38 AM
The following line testLight = GetComponent<Light>();
calls Component.GetComponent()
. As indicated in the documentation, this function returns the Light component from the GameObject this script is attached to.
The following line testLight = testObject.GetComponent<Light>();
calls GameObject.GetComponent()
. As indicated in the documentation, it returns the Light component from the GameObject referenced by the testObject
variable.
thanks for the reply but what is the entity of testObject ? I can understand gameobjects inhierarchy and scene but what are gameobjects in scripts ? so when i have a testObject with a lightcomponent , where the light will be visible ? or it just stores the value of the light ?
i dont know my question is clear or not but im stucked understanding in creating a gameobject inside of a script of a real gameobject in the scene.
Let me take an example :
Supposing you have created a script called LightSwitch
. This script is attached to a gameobject called LightSwitch in your scene. Your player will be able to interact with the LightSwitch gameobject. He can switch it on and switch it off.
The LightSwitch
script will be responsible for turning on and off a Light bulb. To do so, you can reference the LightBulb gameobject in your LightSwitch
script. This script will control whether the Light
component attached to the LightBulb gameobject is on or off.
A GameObject in a script is simply a Variable in the shape of a GameObject. Exactly the same as an int or string or anything, but GameObject shaped. So it'll hold information that looks like a GameObject. Because GameObject is a class it comes with a whole bunch of stuff programmed in to it and you can access all that stuff through your variable. You can either fill it with stuff that looks like a GameObject yourself, like a paper mockup of sorts, or you can make it point with a Reference to an actual GameObject and then you can play around with that Instance. Your Light testLight
is at first simply a variable in the Shape of a Light (which has been created separately). At first its just a 'mockup', just a placeholder, but with GetComponent you can fill that variable with a Reference to an actual Light.
So we use gameobject variables in scripts to reference physical gameobject in the scene or other scenes ans scripts ?
You can reference a gameobject in the scene, if you want to interact with it or a prefab in your Project view if you want to clone (instantiate) it.
Your answer
![](https://koobas.hobune.stream/wayback/20220612152122im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Class for spawning units (gameobjects) 1 Answer
Accessing a method / void from another class? 0 Answers
Is it possible to set variables for classes? 2 Answers
How do I find an external component from inside a class? 0 Answers
How do I instantiate a class Object, then store it in an Array of Class Objects in C#? 1 Answer