About Creating and Destroying objects

Hello guys, i’m really stuck with creating and destroying objects on both server and client side. I’m trying to understand how the Command and RPC’s work on both side. But i only could manage to spawn the object on server&client side, but failed on destroy it. I would be grateful if anyone can help me to understand how i can manage the server or client side. A basic line of code would really help me to understand how it works.

What im trying is to create a cube on both server/client side. Whenever the player clicks on it, destroy the cube on both side.

Ok, so you should be able to achieve that by using some logic like this:

  1. Server Spawn’s the cube on server & client side.
  2. Player chooses a cube to destroy, and send a command asking the server to destroy it.
  3. Server check if that cube is not already destroyed, and sends back a clientrpc to all; asking to destroy that cube.
  4. Voilà!

So, to be able to tell the server witch cube to destroy, what i do is create a “cubeManager” as an example, where i have a list or array of cubes that i spawn by name , then the player sends a cube’s name with a command to the server, then the server finds that name on the list of cubes and then it Sends back a delete clientRpc to all clients to delete that cube.

Do you need a code example?

Thanks for the response, here is what i tryied;

I have a spawn object called “Object Spawn” which has a network identity.

Also i have a GameManager which has the “Object Spawner” script with network identity on it.

On my player prefab i have a script called;
SpawnObject;

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

public class SpawnObject : NetworkBehaviour {

void Update () {
if (!isLocalPlayer) return;
if (Input.GetKeyDown(KeyCode.Alpha5))
GameObject.Find(“GameManager”).GetComponent().SpawnCube();
}
}


ObjectSpawner;

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

public class ObjectSpawner : NetworkBehaviour
{

[SerializeField]
GameObject cubePrefab;
[SerializeField]
GameObject ojectSpawn;

public void SpawnCube()
{
CmdSpawnCube();
}

[Command]
void CmdSpawnCube()
{
GameObject SceneObjects = GameObject.Find(“Object Spawn”);
GameObject Cube = (GameObject)Instantiate(cubePrefab, ojectSpawn.transform.position, Quaternion.identity) as GameObject;
NetworkServer.Spawn(Cube);
Cube.transform.SetParent(SceneObjects.transform);
}

}

To destroy the clicked object;

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

public class DestroyPickedItem : NetworkBehaviour
{
[SyncVar]
public GameObject Cube;
public Vector2 hotSpot = Vector2.zero;

void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 40))
{
CmdDestroyCube();
Cursor.SetCursor(null, Vector2.zero, cursorMode);
}
}
}
[Command]
void CmdDestroyCube()
{
NetworkServer.Destroy(Cube);
}
}

Here is what happens;

Everything works fine by server/host side. But whenever i try to spawn the object on client side it says;
Trying to send command for object without authority.
UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String)
ObjectSpawner:CallCmdSpawnCube()
ObjectSpawner:SpawnCube() (at Assets/Scripts/ItemInteractionSystem/ObjectSpawner.cs:25)
SpawnObject:Update() (at Assets/Scripts/ItemInteractionSystem/SpawnObject.cs:16)

and also when i delete the object, it destroys only on the client side.

I tried to spawn the object using NetworkServer.SpawnWithClientAuthority which cause some other errors on client side.

The idea why i want to learn this is, i want the players drop items to the ground and pick them. It would be really helpful if you could write an example code to solve my situation.