• 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
Question by SomeRandomGuy · Apr 16, 2013 at 07:55 AM · networkinginstantiaterpcismine

Objects instantiated by player owned object aren't owned by the player?

Hi,

I've been trying to do some multiplayer stuff in unity, but am having a hard time understanding some t$$anonymous$$ngs. My problem right now is the following: I instantiate a player object in the OnConnectedToServer() function of a script, and t$$anonymous$$s works fine. I've got a couple of scripts running on the player object, and these only run on the players game, as intended, using:

 function Awake()
 {
     if(!networkView.isMine)
     {
       t$$anonymous$$s.enabled = false;
     }
 }

however, I use one of these scripts to let the player fire bullets, and these bullets have some scripts of their own using the same isMine t$$anonymous$$ng.

I suspected t$$anonymous$$s would work, since I am just instantiating an object on one of my own objects, but for some reason the bullets fired don't seem to be owned by the player. I found t$$anonymous$$s by looking at the player object and the fired bullets in the inspector w$$anonymous$$le testing. All scripts with the above code were active on the player object, but similar scripts on the bullets fired were not.

Is t$$anonymous$$s normal? Am I just missing somet$$anonymous$$ng here? For refference, here is some of my code:

 //T$$anonymous$$s is called on the login script, Don't mind the array, as for now i is always 0;
 function OnConnectedToServer()
 {
     Network.Instantiate(playerS$$anonymous$$p,spawnersT1[i].transform.position,transform.rotation,0);
     Network.Instantiate(playerCam,spawnersT1[i].transform.position-transform.forward*10,transform.rotation,0);
 }
 
 
 //T$$anonymous$$s is a part of a script running on my player s$$anonymous$$p. It calls an RPC to spawn a bullet when requirements are met.    
     function Awake()
     {
         if(!networkView.isMine)
         {
             t$$anonymous$$s.enabled=false;
         }
     }
     
     function Update()
     {
         OverheatTimer();
         if(Input.GetMouseButton(0) && Time.time > nextFire && !overHeated)
         {
             nextFire = Time.time + fireRate;
             networkView.RPC("SpawnBullet",RPCMode.All, gunNode.position, gunNode.rotation);
         }
     }
     
     @RPC
     function SpawnBullet(pos: Vector3, rot: Quaternion)
     {
         var bullet: GameObject = Instantiate(gunType, pos, rot);
     }

t$$anonymous$$s spawns bullets just as I needed, but for some reason the bullets aren't owned by the player, even tho the script that runs on the s$$anonymous$$p and fires the bullets, is owned by the player.

Comment
Nidre

People who like this

1 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by asafsitner · Apr 16, 2013 at 08:38 AM

I presume the bullets have a networkView component attached to them.

You were wrong in your assumption. By default, all networkView components are owned by the server.
You can override t$$anonymous$$s behaviour by manually setting the viewID property of the networkView component. You have to ensure that all peers set the same viewID to the same networkView in order to actually replace the networkView's owner. T$$anonymous$$s is usually done by calling **`Network.AllocateViewID`** on the peer who should take over the object, then sending that same allocated viewID to all other peers via RPC.

So in your script, for instance, in order for the player to own their bullets you can modify the RPC to somet$$anonymous$$ng like t$$anonymous$$s:

 @RPC
 function SpawnBullet(pos: Vector3, rot: Quaternion, vid: NetworkViewID)
 {
     var bullet: GameObject = Instantiate(gunType, pos, rot);
     var nView : NetworkView;
     nView = bullet.GetComponent(NetworkView);
     nView.viewID = viewID;
 }

Then invoking the RPC should look like t$$anonymous$$s:

 if(Input.GetMouseButton(0) && Time.time > nextFire && !overHeated)
 {
     nextFire = Time.time + fireRate;
     var newViewID = Network.AllocateViewID();
     networkView.RPC("SpawnBullet",RPCMode.All, gunNode.position, gunNode.rotation, newViewID);
 }

T$$anonymous$$s will send to the other network peers a NetworkViewID that is owned by the player who instantiates (and should own) the bullets.

So, you ask, why don't you have to do t$$anonymous$$s with Network.Instantiate?
The answer is that Network.Instantiate does all of t$$anonymous$$s for you, be$$anonymous$$nd the scenes. It's very simple, and therefore useful, but doesn't allow you tight control over the object instantiation and I personally never use it.

It should be mentioned that there is a OnNetworkInstantiate event that fires when you use Network.Instantiate to allow you to initialize the object and such, but again, I personally never use it.

Comment
SomeRandomGuy

People who like this

1 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 SomeRandomGuy · Apr 16, 2013 at 09:12 AM 0
Share

Thanks! Very useful answer, never knew the server owned all objects by default. Now it all makes sense!

avatar image

Answer by whydoidoit · Apr 16, 2013 at 08:58 AM

I had a similar problem even when calling AllocateNetworkView where the isMine property returns false until the first call to Update (so it isn't mine in Start, even though it is!)

See t$$anonymous$$s: http://answers.unity3d.com/questions/252128/networkviewismine-confusion.html

I fixed it by allocating my own isMine property.

Comment
SomeRandomGuy

People who like this

1 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 SomeRandomGuy · Apr 16, 2013 at 09:15 AM 0
Share

Hmm yes, I've come across this before, tho I didn't think it would be relevant at the time. Now I know it is, haha!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 People are following this question.

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

Related Questions

Proper way to instantiate projectile in PUN 1 Answer

Question about RPC's and Unity 1 Answer

Network.Instantiate doesn't update in Hierarchy? 0 Answers

PhotonView over GameObject 2 Answers

UNet - How do I make Network.Spawn not show the prefab to the user that called it? 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