Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Edwige · Jan 28, 2011 at 12:57 PM · networknetworkplayer

How to get current NetworkPlayer from a script belonging to it ?

I suppose it should be quite easy to get current Network player from a script attached to it , but I didn't manage to get any answer I understand (and I searched a lot ^^).

In my program, I instantiate a prefab with networkviews when I create a player. I have a script attached to this prefab. How can I know to which networkplayer this script is attached inside this script ?

Now, the only solution I have found is to send a message to this script when I instantiate it, with the Network Player in the arguments, but I am pretty sure there must be easier ways !

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Edwige · Jan 31, 2011 at 01:06 PM

I have found a very easy way to get the local NetworkPlayer when you are in a script : Network.player

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 Bunny83 · Feb 01, 2011 at 10:42 AM 0
Share

You said you want the networkplayer of a certain network view and not the local networkplayer. Anyway, fact is that on clients the only networkplayer you can trust is the server since that's the only one you're connected to. On the server you have all available, cause all are connected to the server. That's why i always setup an authoritative server and everything is managed on the server but that's a lot of work ;)

avatar image
2

Answer by Matthew A · Jan 28, 2011 at 01:52 PM

Not sure if this is entirely what you mean but:

It should be possible to get all the network views attached to the object with:

var views : NetworkView[] = GetComponents(NetworkView);

Then you can iterate through them and use the observed member to get the observed script:

for (view : NetworkView in views) {
    if (this == view.observed) {
        // current view is observing this script
        break;
    }
}
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 Edwige · Jan 31, 2011 at 01:05 PM 0
Share

Thanks for the answer. With your method and the other stated before, I come to that :

NetworkView[] views = GetComponents(); for (int i=0;i

avatar image
1

Answer by Bunny83 · Jan 28, 2011 at 01:26 PM

You can't "attach" something to a networkplayer. The networkplayer is just an identifer for a player on the network. If you instantiate your player prefab with Network.Instantiate and the prefab contains a NetworkView, this NetworkView belongs to the player that called Network.Instantiate.
Inside a script you can get the NetworkView component and check it's owner.

{
    if (networkView.owner == Network.player)
    {

Actually it's not the NetworkView that is owned by the player, it's the NetworkViewID of that NetworkView. This ID links the NetworkViews on all those different clients.


Some further notes:
I don't like Network.Instantiate() because you have no control of the creation and the much more important: the removing of the object. Since Network.Instantiate() uses buffered RPCs it's really hard to removed them selectively. It depends on your network setup. If you don't need an authoritative server it's quite easy. When a player leaves, everything that belongs to that player can be removed. But it still depends on what you need.

Comment
Add comment · Show 5 · 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 Edwige · Jan 28, 2011 at 01:34 PM 0
Share

Ok, this helps me understanding :) But it doesn't really solve the problem : how can I get the networkview the script is attached to, then ? :)

avatar image Bunny83 · Jan 28, 2011 at 01:42 PM 0
Share

Like i did in the scipt above... Every component have some default references to other common components like transform, renderer, rigidbody and networkView (note it's not NetworkView). look here: http://unity3d.com/support/documentation/ScriptReference/Component-networkView.html?from=$$anonymous$$onoBehaviour

avatar image PrimeDerektive · Jan 31, 2011 at 02:46 PM 0
Share

Isn't that essentially the same as using networkView.Is$$anonymous$$ine?

avatar image Edwige · Feb 01, 2011 at 10:18 AM 0
Share

No, I needed the player number :) I have to search an array for an id corresponding to this specific player (the array is created when player is logged in). With Network.player, it works fine.

avatar image Bunny83 · Feb 01, 2011 at 10:36 AM 0
Share

ehmm, you never said anything about your own playermanagement with any numbers or what ever. You always talk about NetworkPlayer and that's a type Unity uses to identify a player on the network. You didn't post any code and it seems you haven't been very specific what you wanna do.

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

No one has followed this question yet.

Related Questions

RPC call ending up on wrong target 1 Answer

How do I make variables update on a network script? 1 Answer

Sending Network player info with RPC 1 Answer

How does this script identify other players? C# 1 Answer

Network.player is -1 for both instances. Unique player id? 1 Answer


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