Unet, ¿Spawn an object on server from an object client with localPlayerAuthority set false?

Hi!

I am having this problem:

“AssingClientAuthority can only be used for Network identity with a localPlayerAuthority set.”

I understand that the objects can are spawned only on server, and I use to the sentence [Command]
to do it:

[Command]
	void CmdScrGunShot(Vector3 argPosition, Quaternion argRotation)
	{
		var tempObject = (GameObject) Instantiate(prefabBullet, argPosition, argRotation);
		tempObject.GetComponent<Rigidbody>().velocity = tempObject.transform.forward*8;
		Destroy(tempObject, 2f);
		NetworkServer.Spawn(tempObject);
	}

I too know that only objects with LocalPlayerAuthority = true; can execute Cmd functions.

then, ¿How do I can spawn an object in server from a object with local player authority set false?

Thanks for helpme!

1 Answer

1

Hi guys!

I find a solution for it, is not the best however, this work good.

I created a class new, how this:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class ClassSpawnerOnServer : NetworkBehaviour 
{
	public GameObject prefabGubBullet;//is for create a new object
	Transform prefabSpawnTranform;//is for set direction to the object instanciated
	bool spawnGunBullet;//if true, instance a new object prefab

	void Update () 
	{
		scrSpawnGunBullet();
	}

	public void scrSetSpawnGunBullet(Transform argSpawnTranform)
	{//this function is called from a object with "isLocalPlayerAuthority = false"
		spawnGunBullet = true;//set true, for create a new object after
		prefabSpawnTranform = argSpawnTranform;
	}

	void scrSpawnGunBullet()
	{//this function is execute one time per frame and check if need create a new object of type prefab
		if(spawnGunBullet)
		{
			CmdScrSpawnGunBulletOnSever(prefabSpawnTranform.position, prefabSpawnTranform.rotation);//Execute the function on server !!"important, this Class is a component of LocalPlayerAuthority = true"
			spawnGunBullet = false;//set false, for not create more objects	
			prefabSpawnTranform = null;
		}	
	}

	[Command]
	void CmdScrSpawnGunBulletOnSever(Vector3 argPosition, Quaternion argRotation)
	{
		var tempObject = (GameObject) Instantiate(prefabGubBullet, argPosition, argRotation);
		tempObject.GetComponent<Rigidbody>().velocity = tempObject.transform.forward*8;
		Destroy(tempObject, 2f);
		NetworkServer.Spawn(tempObject);//spawn on the server
	}
}

any question about it?

Same, any answers?