Your question wasn't clear until your edit then I realized that this is a networking question.
I need to find localPlayer in a NetworkGame where all players have
same name, same tag but only one player is a localPlayer can I access
the localPlayer from another script?
You can find players with NetworkConnection.playerControllers
which retruens List of PlayerController
then loop over it and check if it is valid. After that, get NetworkBehaviour from it check the isLocalPlayer property. That's really it.
Something like this:
GameObject FindLocalNetworkPlayer()
{
NetworkManager networkManager = NetworkManager.singleton;
List<PlayerController> pc = networkManager.client.connection.playerControllers;
for (int i = 0; i < pc.Count; i++)
{
GameObject obj = pc[i].gameObject;
NetworkBehaviour netBev = obj.GetComponent<NetworkBehaviour>();
if (pc[i].IsValid && netBev != null && netBev.isLocalPlayer)
{
return pc[i].gameObject;
}
}
return null;
}
If you have NetworkIdentity
attached to the player instead of NetworkBehaviour
, just get the NetworkIdentity
and check the isLocalPlayer
property. You can also find the GameObject then check the NetworkIdentity
and the isLocalPlayer
property.
if (obj.GetComponent<NetworkIdentity>().isLocalPlayer){}
Other useful player operation you may need:
Find all players:
List<GameObject> ListPlayers()
{
NetworkManager networkManager = NetworkManager.singleton;
List<PlayerController> pc = networkManager.client.connection.playerControllers;
List<GameObject> players = new List<GameObject>();
for (int i = 0; i < pc.Count; i++)
{
if (pc[i].IsValid)
players.Add(pc[i].gameObject);
}
return players;
}
Find player by name(Can also be changed to by tag or layer):
GameObject FindNetworkPlayer(string name)
{
NetworkManager networkManager = NetworkManager.singleton;
List<PlayerController> pc = networkManager.client.connection.playerControllers;
for (int i = 0; i < pc.Count; i++)
{
if ((pc[i].IsValid) && (name == pc[i].gameObject.name))
return pc[i].gameObject;
}
return null;
}
OLD answer before your edit:
Is there other solutions for referencing players in a proper way.
Yes, you Find the GameObject with GameObject.Find
, after this, you can use the GetComponent
function to get the script that is attached to it.
I notice you mentioned you want to reference prefabs in the scene too. There is a difference between prefabs and objects already the scene and they both have different ways to reference them.
Here is an example of Objects in the Scene already. You can see them in the Hierarchy tab:

Let's reference the "Canvas" GameObject by name:
GameObject canvasObj = GameObject.Find("Canvas");
Let's reference the Canvas
script attache to the "Canvas" GameObject:
GameObject canvasObj = GameObject.Find("Canvas");
Canvas canvas = canvasObj.GetComponent<Canvas>();
Prefabs:
Here is an example of prefabs in the Project tab:

Put the prefabs in a folder named "Resources". You must so that you can reference them with the Resources
API.
Let's reference the "Capsule" prefab GameObject:
GameObject prefab = Resources.Load<GameObject>("Capsule");
To use it, you have to instantiate it:
GameObject instance = Instantiate(prefab);
You can only get components on prefabs after when they are instantiated:
CapsuleCollider cc = instance.GetComponent<CapsuleCollider>();