#(noob question) Singleton on a per local player basis

1 messages · Page 1 of 1 (latest)

formal parcel
#

So im new to using mirror and networking so forgive me for any silly questions.

Is is correct to try and make a singleton for every local players, playerStats script? Is is there a better way as I dont really want to constantly be using get component and have like a million stored referances of the same script

radiant pelican
#

NetworkClient.localPlayer.GetComponent<ScriptName>().

#

Is a shortcut
Personally i have one main script that gets set a reference to local player when they spawn in, and all other scripts use that one scripts reference.

#

edit

#

Player script:

public SceneScript sceneScript;

public override void OnStartLocalPlayer()
{
  sceneScript = GameObject.Find(“SceneScript”).GetComponent<SceneScript>();
  sceneScript.playerScript = this;
}

scene script: // a non networked script in the scene

public PlayerScript playerScript;
#

Now all my other scripts can use sceneScript.playerScript for local player access.
You can make the scene script a singleton, or a static variable etc

#

@formal parcel

formal parcel
#

ahj okay so kind of like I have done here already?

#

I created a singleton gamemanager where when the player stats it assignes its playerstats / inventory and have used the GM to access the other scripts

#

@radiant pelican

radiant pelican
#

💪 yeah go for it

formal parcel
#

Perfect thanks!

sick sparrow
formal parcel
#

So would you say

Networkclient.localplayer is better to use?

sick sparrow
#

for cross ref between components on the player, expose fields on the components where you can assign refs to other components in the inspector and assign them at design time or through RequireComponent + OnValidate. This assumes you're not making a massive single Player component, but instead having a bunch of narrow scope components on the player object.

formal parcel
#

Amazing thank you!!

formal parcel
sick sparrow