Reputation: 509
I am currently taking an intro to games programming class and in the lecture notes it mentions that instead of using something like:
GameObject obj = GameObject.Find("name");
inside functions, you can "add object as Public member of type GameObject and connect it in the inspector" without actually explaining how to do it.
I tried to lookup how to do this but was unable to find how to properly do so, would appreciate any help.
Upvotes: 0
Views: 2201
Reputation: 485
Here is how you could do it. Lets say you want obj2
in obj1
First go in the script of obj1
and declare a public
variable public GameObject obj2;
Then go in to the inspector of the obj1
, on script component you will find a field Obj2
. Now just drag and drop the obj2
from hierarchy in that field or click a circular button next to field and select the object from the list.
Upvotes: 1
Reputation: 6441
If you use the public
access-modifier, or for fields with the private
(default), internal
or protected
access-modifier, if you annotate with [SerializeField]
, they will show up in the inspector, if they are serializable and of a supported type.
//This is shown in the inspector
public string _publicField = "public field";
//This is not shown in the inspector, unless you are in debug mode.
private string _privateField = "private field";
//This is shown in the inspector
[SerializeField]
private string _annotatedPrivateField = "annotated private field";
As for what the inspector is:
The Inspector window (sometimes referred to as “the Inspector”) displays detailed information about the currently selected GameObject, including all attached components and their properties, and allows you to modify the functionality of GameObjects in your Scene.
From the official documentation.
Upvotes: 0