Why call GetComponent repeatedly?

Hello Rick and Ben,

Your course is amazing! So far I have understood everything (of course I have had my fair share of bugs but who doesn’t!) and I am enjoying the process of making this game as I have a couple of ideas I would like to try later on.

However, I do have one question right now regarding GetComponent. As far as I knew using GetComponent in a function that gets called repeatedly (such as in Update) is not optimum as it is a resource intensive process. Besides, we only need to call it once to access the component we want. So would not be better to call it in Start and assign the result in a variable of type “RigidBody2D”? Please correct me if I am wrong because I actually got that from one of unity’s free tutorials which, to be honest, are not that great as learning material.

I thank you in advance and after finishing this course I’ll go heads on with the 3D course :smiley:

1 Like

Hello Claudio,

You are correct, there is a cost to using GetComponent, it’s not quite as bad as a Find related method call, but not normally something you would want to call every frame.

Is there a specific piece of code you can share where this occurs, or the time frame from a lecture it is shown in, just to put it in context etc?

This is not my code is from the tutorial i was mentioning (Roll-a-ball tutorial -> Collecting the Pick Up Objects)

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    public float speed;

    private Rigidbody rb;

    void Start ()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

        rb.AddForce (movement * speed);
    }

    void OnTriggerEnter(Collider other) 
    {
        if (other.gameObject.CompareTag ("Pick Up"))
        {
            other.gameObject.SetActive (false);
        }
    }
}

Sorry, I meant a code example from the course where you are saying the GetComponent method is being called repeatedly and unnecessarily.


September 20, 2018 9:08 PM (Europe: Paris), September 20, 2018 12:08 PM (America: Los Angeles)

Do you mean this?

private void LaunchOnMouseClick()
{
	if (Input.GetMouseButtonDown(0))
	{
		hasStarted = true;
		GetComponent<Rigidbody2D>().velocity = new Vector2(xPush, yPush);
	}
}	

If we take a look at the calling method, Update;

void Update ()
{
	if (!hasStarted)
	{
		LockBallToPaddle();
		LaunchOnMouseClick();
	}
}

Whilst this is iterating each frame, it’s checking to see if the game has started, e.g. the ball has been launched, if not, it then calls LaunchOnMouseClick. This is indeed repetition, but LaunchOnMouseClick only calls GetComponent once, when the mouse button is actually clicked, at this point the hasStarted variable is set to true, at the calls to LaunchOnMouseClick will stop due to the if statement within the Update method.

As the Rigidbody component is only ever called once, and nothing else in that class uses the Rigidbody component, having it as a member variable would be unnecessary in this scenario.

If this class did other things with the Rigidbody, then yes, you would be better off creating a member variable to hold a reference to it which the LaunchOnMouseClick and any other methods could access.

Hope this helps :slight_smile:

Ups!! You are completely right. My bad, I forgot about the conditional, so as you said is being called just once. Than you very much.

1 Like

You’re more than welcome :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms