How do I have Reference to GameObject

I’m just learning unity and made a quick script that on click of the item it removes the gameObject. I see that my script inherits MonoBehaviour which not to sure what that is. But my question is how do I have reference to things like gameObject and Destory? How is it that the unity engine is able to provide those items within the scope of my script?

MonoBehaviour is the native Unity class that all scripts naturally inherit from. You can read about “inheritance” in coding, it just means that this current script, which is a class, inherits all the information from another class as if all the code in that other class existed within the current class/script. All the typical Unity methods, functions, etc. exist within the MonoBehaviour class, so if you’re going to use any of that stuff then your script needs to inherit from MonoBehaviour:

Luckily, Unity does this automatically which is why you see “class : MonoBehaviour{” at the top of your scripts. If your script is attached to a gameobject, it MUST inherit from MonoBehaviour. You can have scripts that don’t inherit from MonoBehaviour, but they can’t be attached to a gameobject. This would be like if you created a custom class that you instantiate into other scripts, but don’t get bogged down with that concept yet if you’re just getting started.

“Referencing” is what you have to do if you ever want your script to have control of something other than the gameObject it’s attached to or that gameObject’s Transform component. If you want to do something with some other component on that same gameObject (like a collider, a rigidbody, a light, etc), or if you want to do something with a completely different gameObject and/or it’s components, you must create a reference to that gameObject or component. Creating a reference is just a fancy way of saying that you create a variable to hold the reference to that gameObject or component, and then you use some method to tell the variable what it should be a reference to.

Your variable will be of type “GameObject” if it’s supposed to reference a gameObject, or the type will be a component name if it’s referencing a component (including scripts… the variable type will be the script name). Examples:

GameObject someGameObject;
GameObject someOtherGameObject;
Rigidbody someRigidbody;
Light someLight;
ScriptName someScript;

Now that you have the variables you need, you must tell them what they are. If you make the variables public, like public GameObject someGameObject then it’ll give you a field in the inspector to drag the gameObject to and that will create the reference (you can do the same with component variables… just drag any gameObject that has that component type and it’ll reference the component on that gameObject). Note that you can’t do this if your current gameObject (the one you’re script is on) is a prefab.

If you can’t make it a public variable, then you can assign it a reference in code using some other method. Like just using the gameObject’s name:

someGameObject = GameObject.Find ("GameObjectName");

Or with a “tag”, if the object has that tag applied:

someGameObject = GameObject.FindWithTag ("SomeTagName");

If it’s a component you need to reference, then you need to use the GetComponent() function. Like if the component you’re referencing is on the same gameObject:

someRigidbody = GetComponent<Rigidbody>();

Or if the component is on a different gameObject then you can combine these concepts:

someScript = GameObject.Find ("GameObjectName").GetComponent<ScriptName>();

It seems kinda complicated at first, but once it clicks it’s really simple to use. Just remember the basic rules that if you want to access a different gameobject, or any component other than the Transform component (because it’s static so you don’t have to use GetComponent), you need to create a reference to it. Make a variable to hold your reference, then use the simplest method to assign something to that reference.

Hope that helps!

They are function of MonoBehaivior look here