GetComponent<Transform>().Rotatate() vs. transform.Rotate()

Hi, to make the rocket rotate, we just use transform.Rotate() instead of GetComponent().Rotatate(). I now wonder what the whole point of the GetComponent is? I tried, and it also works but sometimes it seems necessary (e. g. when we were building the obstacle course in the previous lecture) and sometimes it doesn’t (like here) …
Just for understanding purposes :slight_smile: Thanks!

1 Like

Welcome to GameDev Community!

It sounds like you’re referring to two different instances:

  1. Project boost (the rocket) and
  2. The Obstacle Course game (likely the spinners)

Someone else might have a better explanation than this, but I’m thinking that the rocket was controlled by the player using a Mover.cs script of some sort that was directly attached to the rocket.

I don’t recall if the Spinner.cs script was directly attached to the spinners. I don’t think it was, but it’s been a while. I think the script had to go out and ‘find’ the game objects for reference before it could control them.

I think this might be the reason you’re seeing it done two different ways.

I hope this helps.

Welcome to the community @HansImGlueck

The point of GetComponent is to, well, get a component.

All game objects have a Transform component, so it is conveniently included in the MonoBehaviour that your script inherits from. You can certainly use GetComponent<Transform>() to get it, but this is slower than using the already-retrieved transform on the MonoBehaviour.

Unity does not know what other components will be on your game object and can not supply these on the MonoBehaviour. So, this is what you will use GetComponent for. If your rocket has a health component, you can retrieve it using GetComponent<Health>() or whatever your health component’s name is.

1 Like

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

Privacy & Terms