How do I?

I wonder if there is a way to make a static player object?

Wouldn’t this make using that player object simpler? (not having to getcomponent or findobjectoftype)

Thanks

We call this a Singleton. Just for fun, in Google, type “Singletons are” and follow some of the suggestions. :slight_smile: My personal favorite is Singletons are Pathological Liars.

In order to exist in the scene, the player does have to be an instance of a GameObject, with it’s classes bound to the player as MonoBehaviors (you cannot have a static class derived from MonoBehavior.

What you could do is create a dummy Player class and put it in RPG.Core (remember, we’re trying to reduce dependencies, and avoid inversion of dependencies, so what we don’t want is a direct hook to PlayerController if we can help it…

So here is the dummy Singleton Player class, which you can put on the player, and always reference the player with a simple call:

namespace RPG //by using RPG without core, it's available in any RPG.X based class.
{
     public class Player:MonoBehavior
     {
           public static Player Instance => FindObjectWithType<Player>();
     }
}

Then in any RPG. You can reference the player with Player.Instance;

1 Like

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

Privacy & Terms