Destroy synchronization

Options
disign
disign
Why can not destroy object, in all clients?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class Inventory : MonoBehaviour {

    // Use this for initialization
   public List<Item> list;
    GameObject cam;
    public GameObject inventory;
    public GameObject contener;
    public GameObject ty;
    void OnEnable()
    {
        inventory = GameObject.FindGameObjectWithTag("Inventar");
      
        if (!cam && Camera.main)
           
        cam = GameObject.FindGameObjectWithTag("MainCamera");
        // Your characters's camera tag
        if (!cam)
        {
            Debug.Log("Please assign a camera to the ThirdPersonCamera script.");
            enabled = false;
        }
        cam.GetComponent<Camera>();

    }
        void Start () {
        list = new List<Item>();
        inventory.SetActive(false);
    }
	
	// Update is called once per frame
	public void Update () {
        if (Input.GetMouseButtonUp(1))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))

            {
                Item item = hit.collider.GetComponent<Item>();
                if (item != null)
                {
                    list.Add(item);
                    ty = hit.collider.gameObject;
                    GetComponentInParent<PhotonView>().RPC("Spawn", PhotonTargets.All, null);
                    
                }

            }

        }
        if(Input.GetKeyUp(KeyCode.I))
        {
            if (inventory.activeSelf) 
            {
                inventory.SetActive(false);
                for (int i = 0; i < inventory.transform.childCount; i++)
                {
                    if (inventory.transform.GetChild(i).transform.childCount > 0)
                    {
                     Destroy(inventory.transform.GetChild(i).transform.GetChild(0).gameObject);
                    }
                }
            }
            else
            {
                inventory.SetActive(true);
                int count = list.Count;
                for (int i = 0; i < count; i++)

                {
                    Item it = list[i];
                    if(inventory.transform.childCount>=i)
                    {
                       
                        GameObject img = Instantiate(contener);
                        img.transform.SetParent(inventory.transform.GetChild(i).transform);
                        img.GetComponent<Image>().sprite =Resources.Load<Sprite>(it.sprite);
                        img.AddComponent<Button>().onClick.AddListener(() => remove(it, img));
                        
                    }
                    else break;
                }
            }
        }
		}
    void remove(Item it, GameObject obj)
    {
        GameObject newo = Resources.Load<GameObject>(it.prefab);
        PhotonNetwork.InstantiateSceneObject(newo.name, Vector3.up, Quaternion.identity, 0, null);
        newo.transform.position = transform.position + transform.forward + transform.up;
        Destroy(obj);
        list.Remove(it);
    }

    [PunRPC]
    private void Spawn()
    {
        
      
       PhotonNetwork.Destroy(ty);

    }
}

Comments

  • Hi disign,

    RPC("Spawn", PhotonTargets.All, null);
    [PunRPC]
    private void Spawn()
    {
    PhotonNetwork.Destroy(ty);
    }
    don't do that. PhotonNetwork.Destroy(...) only needs to be called by one client, mostly by the one who created the game object or currently owns it or the Master Client if it is a scene object. The networked object is then automatically destroyed on all connected clients. Also you might think about the naming in your code: Function 'Spawn' means Destroy and 'remove' means InstantiateSceneObject is very confusing in my opinion.
  • disign
    Options
    I have altered but still does not work


    GetComponentInParent().RPC("des", PhotonTargets.MasterClient, null);
    [PunRPC]
    private void des()
    {


    Destroy(ty);

    }
    function 'remove' means InstantiateSceneObject and destroy image object in inventory
    sorry for my English i am from Ukraine
  • Is this inventory per player or does every player share the same inventory? In the first case, you don't need to add networking logic to it, since no other client might be interest in what other players carry in their inventory. In the second case you need to handle this use case in a different way, but first please confirm, that you need a single inventory for all connected players.
  • disign
    disign
    edited November 2016
    Options
    each of them has its own invertar, but any other player to see how the player takes the object, and object in scene disappears
  • Okay, good to know.

    As mentioned before you don't need to synchronize the inventory itself in this case. For the pickup of an object, I would recommend you taking a look at the Pickup Demo from the PUN package. The pickup mechanic is mostly, what you want to have. You only have to do some adjustments, so that the object is destroyed on pickup.
  • disign
    Options
    ty = hit.collider.gameObject;
    il = hit.collider.GetComponent().viewID;
    hit.collider.GetComponent().RPC("DestroyRpc", PhotonTargets.All, null);

    [PunRPC]
    public IEnumerator DestroyRpc()
    {
    GameObject.Destroy(ty);
    yield return 0; // if you allow 1 frame to pass, the object's OnDestroy() method gets called and cleans up references.
    PhotonNetwork.UnAllocateViewID(il);
    }

    Thank you, I found the script of the package PUN .This solved my problem.But I would like to make the instantiations of objects.but it does not work :
    newo = Resources.Load(it.prefab);
    al = newo.GetComponent().viewID;
    newo.transform.position = transform.position + transform.forward + transform.up;
    newo.GetComponent().RPC("invt", PhotonTargets.All, null);
    public IEnumerator invt()
    {

    [PunRPC]
    GameObject.Instantiate(newo, Vector3.up, Quaternion.identity);
    yield return 0; // if you allow 1 frame to pass, the object's OnDestroy() method gets called and cleans up references.
    PhotonNetwork.UnAllocateViewID(al);
    }

    gives an error message

    Illegal view ID:0 method: invt GO:Sphere
    UnityEngine.Debug:LogError(Object)
  • Your [PunRPC] attribute is inside the function, but you need to place it before the function declaration itself.
    [PunRPC]
    void SomeFunction() {}
    However this doesn't solve the problem. Error message tells you, that you don't have a valid viewID on 'newo' object. This is because you are trying to instantiate a network object manually. To do so you need to call AllocateViewId() on the created object, before doing anything else. Also you need to adjust your code as well. If you want to use manual instantiation, I recommend you reading through this guide.