Why FindObjectOfType PlayerMovement , not NameOfThePlayer or similar

Hi,

I’m a bit confused about the use of “FindObjectOfType” in this case.

As I undstand it, it searches for an object of Type “PlayerMovement” which is unique in this case. As I see it, the object is a script, which is attached to the player.

We then assign that script to the variable “player”, which is of type PlayerMovement.

But how can we access the transform information of our player, if we are working with a script right now?

xSpeed = player.transform.localScale.x * bulletSpeed;

Why don’t we have to find a variable of type “GameObject” called player = FindObjectOfType<NameOfThePlayer>(); ? Then access its transform and change the local scale there?

Confused user gives his thanks :slight_smile:

2 Likes

All the scripts that we can attach to game objects derive from MonoBehaviour. That means they are of type MonoBehaviour. Because the devs at Unity realised that the transform is something that you will be accessing quite often in very many of the attached scripts, they have conveniently added a field in MonoBehaviour that references the transform of the game object, so you don’t have to try and find it. So, all scripts we attach to game objects already have a reference to the transform.

1 Like

Hey Bixarrio, thanks for the reply.
So that means that this approach only works if we actually search for a script? We couldn’t search for another component like the player input or a collider?

Also: if I wanted to access the player directly, what would the apporach be?
I don’t seem to be able to do something like:

PlayerName myPlayer;
later:
myPlayer = FindObjectOfType<PlayerName>();

Wouldn’t that be analogous to our example?

Thanks :slight_smile:

1 Like

You can find any component that is attached to a game object.

What do you mean ‘the player directly’? What is ‘the player’? The player is everything that makes up that thing; Colliders, Rigidbodies, MeshFilters, MeshRenderers, SpriteRenderers, etc. Any one of those gives you ‘the player’.

You can do this. If you have a PlayerName script attached to the player.

I don’t quite understand what you are trying to achieve. Any component that is on the player is part of the player and through it, you have access to any other part of the player.

1 Like

Hey Bixarrio, thanks for the reply.

What I’m trying to achieve, is to understand the relations between different concepts in Unity. I’m having a hard time sometimes keeping up with all the new concepts, it’s not a trivial thing for a newbie :slight_smile:

I used to think (before this conversation), that something you place in the world like a sprite (e.g. 2D Object → Sprite → Circle) or an instance of a prefab (my player), was a game object.
Also that you can have different components (like a sprite renderer, a transform, an attached script, rigid body, etc.) attached to this game object.

Note: part of what I wrote was deleted when I posted it, probably because of a formatting thing from the editor?. The inequality signs where all deleted. Maybe this caused a missunderstanding between us.

I thought to remember that if you have a game object, like a circle, you could access its components:

[SerializeField] GameObject myCircle; // You link your cicle in the inspector
to access it’s transform: myCircle.transform
to access it’s local scale’s x-component: myCircle.transform.localScale.x

Since I seem to be mistaken, it caused confusion about the concepts.

So for my understanding: you could find any component that is attached to my player object (instance of my player prefab in the world) and access my the transform information through that component? Like if there was only one RigidBody2D in my game, could I find my player through this? // if my player is an object at all

I meant the thing I placed in the scene, the thing I believed to be a game object and an instance of my prefab. The thing that is called “Ginger” in the video.

This seems to contradict what you said above. I asked if you can search for the player through other components, and you answered with “any component that is attached to a game object”.
Again, maybe just a missunderstanding because of my broken code :slight_smile:

Thank you very much

Edit: I fixed all the code I wrote above, to include everything that was deleted

I think I have confused you a lot now. I’m sorry. I’ll try to explain it a little better

You used to think correctly. Everything is a game object and we attach components to game objects to make them be and do what we want. Without components, they are just empty game objects.

This is correct. And you can also link a component and do the same thing

[SerializeField] Rigidbody2D myCircle;
//to access it’s transform:
myCircle.transform
//to access it’s local scale’s x-component:
myCircle.transform.localScale.x

Like I said above, all components have a reference to the transform

I didn’t say that.

Yes. If there is only one Rigidbody2D in the game, and that Rigidbody2D is attached to your player game object, you can use FindObjectOfType<Rigidbody2D>() and it will give you the Rigidbody2D that is attached to your player game object. And, like I said above, all components have a reference to the transform so you do have access to the transform

Seems like a misunderstanding. I thought you wanted to find components on your player, not find your player in the game.

You can find your player in the game by looking for any component that is on your player, as long as that component does not exist on any other game object in the scene. If you have only one Rigidbody2D and it is on your player, you can find your player with it. As soon as you add another Rigidbody2D, you have no guarantee that it will be your player anymore.

1 Like

Continuing the discussion from Why FindObjectOfType PlayerMovement , not NameOfThePlayer or similar:

Why can’t we have a more direct reference to Ginger (the player object with the many components attached)? It seems like an indirect route to look for a Unique Component (which is a component of our Player), and check its Transform becasue Transform = player transform.

Like what would we do if there wasn’t a unique component on Ginger, for us to FindObjectOfType with?

Thanks!

Hi Joshua,

I moved your post to the linked thread and re-opened the thread.

In that case, we either have to look for the name “Ginger” (GameObject.Find), or we have to assign the reference manually in the Inspector. To be able to assign the reference manually, we would use the [SerializeField] attribute.

Is this what you wanted to know?

It is unlikely that you will have a player object with no unique component on it. You need to control your character. That is pretty unique because you will likely not be controlling any other character. And if you do control more than one character - like in the Turn-Based Strategy course - there are different ways of accessing the individual player units.

Perfect, yes that’s what I was looking for. Good to know multiple ways to execute the same behavior!

For player, sure, but it’s pretty easy to imagine a setup where you want to control specific objects that don’t have unique scripts attached to them. That turn-based strategy example sounds interesting, I’ll have a look once I finish up Unity 2D and 3D.

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

Privacy & Terms