Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by KungKras · Jan 19, 2016 at 08:25 AM · unity 5networkingplayer

How do I find the player GameObject of the local player?

Hello! I've been playing around with network code for a while now to try and make a multiplayer RTS. So far I'm only testing concepts, to find a structure that works before committing to making a game.

I've realized that I want the units to keep track of an empty player GameObject that contains a script with player information, like player number, colour etc. This player GameObject spawns when hosting or joining a game because I'm using the basic NetworkManager at the moment.

I've been doing this in order to find the local player in the UnitTest class:

 public void FindLocalPlayer() {
         foreach(GameObject cur in GameObject.FindGameObjectsWithTag("Player")) {
             Debug.Log (cur);
             Debug.Log ("Iterating");
             if(cur.GetComponent<RTSPlayerTest>().isLocalPlayer == true) {
                 Debug.Log ("Found local player");
                 localPlayer = cur.GetComponent<RTSPlayerTest> ();
             }
         }
         if (localPlayer == null) {
             Debug.Log ("Couldn't find local player");
         }
     }

but I always get the "couldn't find local player" message, which baffles me because this code in the RTSPlayerTest class, that's supposed to assign player numbers functions in the same way but works perfectly:

 int i = 1;
         foreach(GameObject cur in GameObject.FindGameObjectsWithTag("Player"))
         {
             if (i <= cur.GetComponent<RTSPlayerTest> ().number) {
                 i++;
             } else {
                 number = i;
                 Debug.Log ("Player number assigned: " + number);
                 break;
             }
         }

If this has already been asked I apologize, but I couldn't find anything similar on google.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KungKras · Jan 17, 2016 at 07:58 PM 1
Share

It seems like there was nothing wrong with my code. After checking the inspector, I see that my player prefabs don't spawn, which is weird because I'm using the stock Network$$anonymous$$anager. Should I delete the question since it's obsolete, or will my code snippets posted here be of any help to someone else wanting to find th local player?

avatar image stjernerlever KungKras · Feb 05, 2019 at 01:14 PM 0
Share

Please keep it here :)

2 Replies

· Add your reply
  • Sort: 
avatar image
10

Answer by FatmaGurel · Dec 01, 2017 at 10:57 PM

I don't know if this is the right way to do this, but what I do is in Player Controller Script I do the following:

 public override void OnStartLocalPlayer()
 {
      base.OnStartLocalPlayer();
      gameObject.name = "Local";
 }

And in my Game Manager I use GameObject.Find("Local") to do things thereon.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Graithen · May 26, 2019 at 03:49 PM 0
Share

This makes referencing the local player so much easier! Thanks a bunch for this idea! :)

avatar image wlwl2 · Jan 24, 2021 at 09:27 PM 0
Share

This solves not only the original problem but identifying a player over a network as well. Good work!

avatar image
3

Answer by UDM1983 · Jan 20, 2017 at 10:53 AM

If you assigned a NetworkIdentity component on your player prefab, you can try:

 if (cur.GetComponent<NetworkIdentity>().isLocalPlayer)
 {
 //do whatever you wish here
 }

Did not test it myself though.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image stjernerlever · Feb 05, 2019 at 01:20 PM 0
Share

But what if you need to know the difference between four players?

So you save that

player 1 is called Bob

player 2 is called Annie

player 3 is called Sylvester

player 4 is called Arnold

To save that your four players have different names, you'd need to give them each a different ID, not just true/false LocalPlayer. I'm guessing there is a Network function for this

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how do i network sync scaling of character? 1 Answer

Child of a player not syncing position with network? 0 Answers

uNet horrible lagg (even on local servers) 0 Answers

Chat system is not working 1 Answer

uNet Offline and Online Scenes the Same 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges