Access Player Using HookPlayer
How to access the player from your own scripts using C#
The documentation has moved to: https://mitschmr-studios.io/documentation/api-guides/accessplayerusinghookplayer.html
This version will no longer be updated and maintained.
In this tutorial, I want to show you how to access the player (gameobject and components) using the HookPlayer component.
Prerequisites
Install Unity
Install IDE (I use Visual Studio 2019)
Import Game Creator
Accessing the Player
Setup
There are several ways to access the Game Creator player (gameobject and components). I have shown you two ways on how to access a component on a gameobject in the previous tutorial, Access GC Script Components. This here works very the same. I can either add a public gameobject to the script and search for a component, or reference the component directly:
But let's start from the beginning. Make a new script with the name TutorialHookPlayer
. Delete everything unused (voids and using statements). The script should look like the following:
Add the using
statements for Game Creator Character and Hooks:
Now we are ready to go.
Access the player gameobject
As I wrote above, we have already covered the part of accessing components of a gameobject. We will look at this again in a later section of this tutorial. I want to focus on accessing the player gameobject. We can do it like in the previous tutorial, with referencing a gameobject:
This is the easiest way to access any gameobject, not just the player. Drag and drop the player from the Unity hierarchy into the field and you are good to go. But Game Creator allows another way to access the player directly, without needing to reference it like this. This is where Hooks
come into the game. As stated in the Game Creator documentation:
Hooks are Unity components that allow you to easily access unique objects such as the Player or the Main Camera. Their use is not required but useful if you want to simplify the process of selecting common objects.
Let's do this then. First, make sure that your player has the HookPlayer
component on it. This is done by default when adding a player to your scene, we just make sure it is there.
We then can grab this component by script using the following line of code:
You might ask yourself now, but your variable is of type IHook
, this is different than referencing the player gameobject. I agree with you, but to change it, we only need to do this:
You see the difference? The IHook
allows us to access the gameobject too. This is the same as when referencing the player by a public gameobject.
Access the player components
We have now seen another way to access the player gameobject. Let's use this knowledge for accessing the players components. There are now at least three ways to do it. Here is an example for accessing the PlayerCharacter component:
You don't need to use the gameobject
field for accessing a component. Using HookPlayer.Instance
is enough. Accessing every other component is the same process as with PlayerCharacter
, just replace with what you are searching for and everything is good.
Last updated