void deserializeMonsters(byte[] monsterBytes)
    {
        // DEBUG
        //string x = "";
        //for (int i = 0; i < monsterBytes.Length; ++i) { x += monsterBytes[i]; }
        // UnityEngine.Debug.Log(playerId + " monsterbytes in: " + x);
        // DEBUG
        int index = 0;
        int numMonsters;

        Protocol.Deserialize(out numMonsters, monsterBytes, ref index);
        HashSet <int> newSerializeIds = new HashSet <int>();

        for (int i = 0; i < numMonsters; ++i)
        {
            int serializeId;
            Protocol.Deserialize(out serializeId, monsterBytes, ref index);
            newSerializeIds.Add(serializeId);
            int monsterId;
            Protocol.Deserialize(out monsterId, monsterBytes, ref index);
            Monster monster;
            if (monsterRef.TryGetValue(serializeId, out monster))
            {
                if (monster.monsterId != monsterId)   // Destroy if wrong monster.
                {
                    destroyMonster(monster);
                    monster = null;
                }
            }
            if (monster == null)   // No monster or wrong (destroyed) monster
            {
                monster = Instantiate(MonsterR.getById(monsterId));
                monster.transform.SetParent(viewMapRef.transform);
                monster.gameState = this;
                monster.SetPath(viewMapRef.getPath());
                monster.serializeId     = serializeId;
                monsterRef[serializeId] = monster;
            }
            // There is now a valid monster. Update
            monster.deserializeFrom(monsterBytes, ref index);
        }
        // Remove no longer existing monsters.
        List <Monster> toDestroy = new List <Monster>();

        foreach (KeyValuePair <int, Monster> pair in monsterRef)
        {
            if (!newSerializeIds.Contains(pair.Key))
            {
                toDestroy.Add(pair.Value);
            }
        }
        foreach (Monster m in toDestroy)
        {
            destroyMonster(m);
        }
    }
 public void spawnMonster(int monsterId)
 {
     if (photonView == null || photonView.isMine)
     {
         Monster monster = Instantiate(MonsterR.getById(monsterId));
         monster.gameState = this;
         monster.SetPath(viewMapRef.getPath());
         monster.serializeId             = ++monsterCount;
         monsterRef[monster.serializeId] = monster;
         SoundManager.instance.PlaySpawnMonster();
     }
 }
    // Use this for initialization
    void Start()
    {
        PlayMap   playMap = Object.FindObjectOfType <PlayMap>();
        Transform grid    = transform.GetChild(0);

        foreach (Monster monsterPrefab in MonsterR.getAllMonsters())
        {
            GameObject monsterbtn = Instantiate(MonsterButton);
            monsterbtn.name = monsterPrefab.name + " button";
            monsterbtn.transform.SetParent(grid, false);
            monsterbtn.transform.localScale          = new Vector3(1, 1, 1);
            monsterbtn.GetComponent <Image>().sprite = monsterPrefab.GetComponent <SpriteRenderer>().sprite;
            monsterbtn.GetComponent <Image>().color  = monsterPrefab.GetComponent <SpriteRenderer>().color;
            monsterbtn.GetComponent <Button>().onClick.AddListener(delegate { playMap.onMonsterBtnClick(monsterPrefab); });
            monsterbtn.transform.GetChild(0).GetComponent <Text>().text = "$" + monsterPrefab.price;
        }
    }