简体   繁体   中英

Why is my photon pun code returning "null?"

Basically says it in the title... can't seem to figure out why player properties is returning null... I'm using Unity 2019.24 and Photon PUN... I'm trying to get it to return a random string out of an array and I'm trying to get it to where no one person has the same string...


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;

public class PropertyAssigner : MonoBehaviour
{
    public SpriteRenderer Donkey = new SpriteRenderer();
    public SpriteRenderer Elephant = new SpriteRenderer();
    public SpriteRenderer Porcupine = new SpriteRenderer();
    public SpriteRenderer Beaver = new SpriteRenderer();
    public SpriteRenderer Eagle = new SpriteRenderer();
    public SpriteRenderer Bird = new SpriteRenderer();
    public SpriteRenderer Dinosaur = new SpriteRenderer();
    public SpriteRenderer Rhino = new SpriteRenderer();
    public Sprite DonkeySp;
    public Sprite ElephantSp;
    public Sprite PorcupineSp;
    public Sprite BeaverSp;
    public Sprite EagleSp;
    public Sprite BirdSp;
    public Sprite DinosaurSp;
    public Sprite RhinoSp;
    
    List<string> icons = new List<string>();
    List<string> shuffledIcons = new List<string>();
    bool b;
    ExitGames.Client.Photon.Hashtable playerProperties = new ExitGames.Client.Photon.Hashtable();
    
    // Start is called before the first frame update
    void Start() 
    {
        var hash = new Hashtable();
        icons.Add("Donkey");
        icons.Add("Elephant");
        icons.Add("Porcupine");
        icons.Add("Beaver");
        icons.Add("Eagle");
        icons.Add("Bird");
        icons.Add("Dinosaur");
        icons.Add("Rhino");
        playerAnimalAssign();
    }
    
    // Update is called once per frame
    void Update()
    {
    }
    
    void SetCustomProperties(string icon)
    {
        Debug.Log("setting properties");
        ExitGames.Client.Photon.Hashtable customProperties = new ExitGames.Client.Photon.Hashtable();
        
        if (!customProperties.ContainsKey("icon"))
        {
            Debug.Log("adding properties");
            customProperties.Add("icon", "icon");
        }
        
        customProperties["icon"] = "icon";
        Debug.Log(customProperties.ToStringFull());
        PhotonNetwork.LocalPlayer.SetCustomProperties(customProperties);
        Debug.Log(customProperties.ToStringFull());
        foreach (var item in PhotonNetwork.PlayerList)
        {
            if (item.CustomProperties.ContainsKey("icon"))
            {
                Debug.Log(item.CustomProperties["icon"]);
            }
        }
    }
    
    void playerAnimalAssign()
    {
        for (int i = 0; i < icons.Count; i++)
        {
            Debug.Log(icons.Count);
            string temp = icons[i];
            int randomIndex = Random.Range(i, icons.Count);
            icons[i] = icons[randomIndex];
            icons[randomIndex] = temp;
        }
        string s = icons[1];
        playerProperties.Add("icon", s);
        playerProperties["icon"] = s;
        
        Debug.Log(playerProperties.ToStringFull());
        
        SetCustomProperties(s);
        icons.Remove(icons[1].ToString());
        
        Debug.Log(PhotonNetwork.LocalPlayer.CustomProperties["icon"]);
        Debug.Log(icons.ToStringFull<string>());
        Debug.Log(PhotonNetwork.PlayerList.Length);
        Debug.Log(PhotonNetwork.LocalPlayer.CustomProperties["icon"]);
        
        foreach (var item in PhotonNetwork.PlayerList)
            if (item.CustomProperties["icon"] == "Donkey")
            {
                Donkey.sprite = DonkeySp;
                
                Debug.Log("Donkey Sprite");
            }
            else if (item.CustomProperties["icon"] == "Elephant")
            {
                Elephant.sprite = ElephantSp;
                
                Debug.Log("Elephant Sprite");
            }
            else if (item.CustomProperties["icon"] == "Porcupine")
            {
                Porcupine.sprite = PorcupineSp;
                
                Debug.Log("Porcupine Sprite");
            }
            else if (item.CustomProperties["icon"] == "Beaver")
            {
                Beaver.sprite = BeaverSp;
                
                Debug.Log("Beaver Sprite");
            }
            else if (item.CustomProperties["icon"] == "Eagle")
            {
                Eagle.sprite = EagleSp;
                
                Debug.Log("Eagle Sprite");
            }
            else if (item.CustomProperties["icon"] == "Bird")
            {
                Bird.sprite = BirdSp;
                
                Debug.Log("Bird Sprite");
            }
            else if (item.CustomProperties["icon"] == "Dinosaur")
            {
                Dinosaur.sprite = DinosaurSp;
                
                Debug.Log("Dinosaur Sprite");
            }
            else if (item.CustomProperties["icon"] == "Rhino")
            {
                Rhino.sprite = RhinoSp;
                
                Debug.Log("Rhino Sprite");
            }
    }
}

thank you in advance for your help!! And if you see any spaghetti code, feel free to tell me ^w^

Setting properties is asynchronous and won't take effect on the client directly after making the method call even if it returns true . You need to wait for the server to set them and "ack" with an event to sync. the update on all clients joined to the same room. Read more here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM