Best way to find player in the scene?

In Unity I noticed most of my C# script had the same code that would look for the player in the scene. To get rid of repetitive code I decided to make a game object called FindPlayerScript and have attached the following script in the link below. The FindPlayerScript would then be passed to other game objects with scripts that would take the FindPlayerScript as a private or public GameObject. The scripts would then be able to access the components to modify or receive any values needed.

I just started with Unity and this is the best method I could think up. Are there better options or should I continue what I’m doing?

I’m using a link because I couldn’t find a format option for code.

Personally, in my games no one knows who the player is. So for me I have an abstract base class script called Actor with some public virtual methods for say taking dmg etc. The reason I do this is because if you think about it, there should be no reason for an Enemy script to have direct access to a player at all times I just don’t see the purpose for this. Instead my AI has a Actor m_target to focus on something, if its the player ok if not then fine too. When someone dies it reports it to the GameMGR like so GameMGR.SceneInstance.ActorKilled(Actor:killed, Actor:killer) and I can then continue on to check points etc. This gives me more than enough info and each object never cares about whether its attacking the player or not.

4 Likes

You could make you player a singleton object.

public class Player : MonoBehaviour
{
    static Player instance;
    public static Player instance
    {
        get
        {
            return _instance;
        }
    }
  
    void Awake()
    {
        _instance = this;
    }

}

Then you can access your player object anywhere in your project through:

Player.instance
1 Like

The issue becomes what if you wanted to have multiple players? Whats the design pattern for that?

1 Like

This seems like a good idea since the game I am making is strictly single player. However I will consider Polymorphiks’s question about multiplayer when and if I make a game with such functionality

A singleton with a list of all active players :wink:

Ultimately this question is highly dependent on the game type. I’m not sue making the player a singleton is a good idea. You don’t want any object to be able to grab you player and do stuff.

But making a globally accessible object that can provide access to important data from your player might be viable.

1 Like

:smile: Yes, that’s the multiton! (In fact you have variation of that design pattern such as: dualton, tripleton, quadrulpleton, …, N-ton).

Then, I would implement the multiton this way:

using UnityEngine;
using System.Collections.Generic;

public class Player : MonoBehaviour
{
    static Player[] _instances;
    static List<Player> _instanceList;
    public static Player[] instances
    {
        get
        {
            if (_instances == null)
                _instances = _instanceList.ToArray();
            return _instances;
        }
    }

    void Awake()
    {
        _instanceList.Add(this);
        _instances = null;
    }

    void OnDestroy()
    {
        _instanceList.Remove(this);
        _instances = null;
    }
}

This is not limited to players. You can use this pattern as it may suits your problem (It’s a design pattern after all). Take care of not abusing it though.

1 Like