Hi there, I’m (finally) learning how to program. I’ve been at it for a few months, and I’m still highly confused by getComponent, I’m stuck on the easiest stuff, haha. Audio playing is one, which I’m honestly still surprised things don’t figure themselves out. Then again… This is programming. But, thanks!

@MarkedMaps you use GetComponent to get a reference to a Component on a GameObject.

A simple example would be this:

  1. You have created a MonoBehaviour script called “MySpecialFunctionalityScript”, and you have added this script to a GameObject in the Scene, called “Player”, which also has a script on it called “Player”.
  2. Inside the Player script you need a reference to the “MySpecialFunctionalityScript”, because it has a public method that you need to call “MyCustomMethod”.

This is how you would do it:

using UnityEngine;

//This is the Component that will be added to the Player GameObject
public class MySpecialFunctionalityScript : MonoBehaviour {

    public void MyCustomMethod()
    {
        //Do something here...
    }

}


using UnityEngine;

public class Player : MonoBehaviour
{
    private MySpecialFunctionalityScript mySpecialFunctionality; // this variable will hold a reference to the component

    void Awake()
    {
        //Get the reference to the component
        mySpecialFunctionality = GetComponent<MySpecialFunctionalityScript>();
    }

	// Use this for initialization
	void Start () {

        //Call the public MyCustomMethod
        mySpecialFunctionality.MyCustomMethod();

	}
	
}

Since you are new to Unity and programming, you might find very helpful the Unity Tutorials, which have a “Scripting” section:

https://unity3d.com/learn/tutorials

https://unity3d.com/learn

IF YOU WANT TO USE GET COMPONENT, BE CAREFUL.


Based on my experiences, GetComponent is a trap. But it is a trap just if you don’t use it right. I used to use GetComponent somewhat like every 5 lines. I have 100+ scripts. All of them had GetComponent in it.

I am not an expert. I am just a guy that uses it in a way that doesn’t harm my system. Actually, I barely use it. Only under specific cases, which is what I recommend you to do.

But I will not tell you how does it work. I can’t explain it by myself, it would get hell confusing… XD

Plus, I don’t fully understand it.

To know how does it work, read this article, it is great.

BUT

GetComponent WILL slow down your game IF NOT USED CORRECTLY.

Use it on Update or LateUpdate and you’ll have a GetComponent returning a variable EVERY SINGLE FRAME… This is bad, mainly because GetComponent has some checks to avoid errors (just like Update does, which makes it better to not have dozens of Updates), and the way it use to get the components. Again, I’m not getting into details here. For further explanation check the link provided, please.

It’s easy to avoid issues: Only use it under specific conditions (Start(), Awake(), after a casual check) and if not possible, cache it. Store it on memory, so you can save a little bit of CPU per use.