- Home /
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 things. My problem right now is the following: I instantiate a player object in the OnConnectedToServer() function of a script, and this 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)
{
this.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 thing.
I suspected this 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 this by looking at the player object and the fired bullets in the inspector while testing. All scripts with the above code were active on the player object, but similar scripts on the bullets fired were not.
Is this normal? Am I just missing something here? For refference, here is some of my code:
//This is called on the login script, Don't mind the array, as for now i is always 0;
function OnConnectedToServer()
{
Network.Instantiate(playerShip,spawnersT1[i].transform.position,transform.rotation,0);
Network.Instantiate(playerCam,spawnersT1[i].transform.position-transform.forward*10,transform.rotation,0);
}
//This is a part of a script running on my player ship. It calls an RPC to spawn a bullet when requirements are met.
function Awake()
{
if(!networkView.isMine)
{
this.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);
}
this 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 ship and fires the bullets, is owned by the player.
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 this 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. This 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 something like this:
@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 this:
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);
}
This 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 this with Network.Instantiate
?
The answer is that Network.Instantiate
does all of this for you, behind 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.
Thanks! Very useful answer, never knew the server owned all objects by default. Now it all makes sense!
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 this: http://answers.unity3d.com/questions/252128/networkviewismine-confusion.html
I fixed it by allocating my own isMine property.
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
Follow this Question
Related Questions
Deleting RPC objects 1 Answer
network.instantiating players using RPC calls 1 Answer
Proper way to instantiate projectile in PUN 1 Answer
Question about RPC's and Unity 1 Answer