Access Player Using HookPlayer

How to access the player from your own scripts using C#

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:

public GameObject playerGo;
public PlayerCharacter playerComponentPc;
void Method()
{
PlayerCharacter pc = playerGo.GetComponent<PlayerCharacter>();
}

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:

using UnityEngine;
public class TutorialHookPlayer : MonoBehaviour
{
}

Add the using statements for Game Creator Character and Hooks:

using GameCreator.Characters;
using GameCreator.Core.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:

public GameObject playerGo;

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:

// IHook player = HookPlayer.Instance;
var player = HookPlayer.Instance;

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:

public GameObject playerGo: // Method 1
//Method 2
void GetPlayerGameObject()
{
GameObject playerGo2 = HookPlayer.Instance.gameObject; // same as 1
}

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:

Method 1
Method 2
Method 3
Method 1
public GameObject player;
void GetComponentsMethod1()
{
// var component = player.GetComponent<PlayerCharacter>();
PlayerCharacter component = player.GetComponent<PlayerCharacter>();
// Do stuff with component.xxx
}
Method 2
public PlayerCharacter player;
void GetComponentsMethod2()
{
// Do stuff with player.xxx
}
Method 3
void GetComponentsMethod3()
{
PlayerCharacter player = HookPlayer.Instance.GetComponent<PlayerCharacter>();
// PlayerCharacter player = HookPlayer.Instance.gameobject.GetComponent<PlayerCharacter>();
// Do stuff with player.xxx
}

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.

If the tutorial was useful to you, consider supporting me on Patreon: https://www.patreon.com/MiTschmrStudios