Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. If you're a macOS 10.13 High Sierra user take be sure to read this before updating Unity.
    Dismiss Notice
  5. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  7. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Unity Multiplayer [PHOTON] Getting NullReference when theres more players

Discussion in 'Multiplayer Networking' started by Moddwyn16MC, Dec 7, 2017.

  1. Moddwyn16MC

    Moddwyn16MC

    Joined:
    Jan 27, 2016
    Posts:
    43
    Code (CSharp):
    1. void Start(){
    2.         killFeedUI = GameObject.Find ("KillFeed");
    3.     }
    Code (CSharp):
    1. public void Die(){
    2.         GameObject[] players = GameObject.FindGameObjectsWithTag ("Player");
    3.         foreach(GameObject p in players){
    4.             p.GetComponent <PhotonView>().RPC("SendFeed", PhotonTargets.All, killersName);
    5.         }
    6.         PhotonNetwork.Destroy (gameObject);
    7.     }

    Code (CSharp):
    1. [PunRPC]
    2.     public void SendFeed(string killer){
    3.         GameObject newFeed = GameObject.Instantiate (feedPrefab);
    4.         newFeed.transform.SetParent (killFeedUI.transform);
    5.         newFeed.transform.GetChild (0).GetComponent <Text>().text = (killer + " killed " + GetComponent <PhotonView>().owner.NickName);
    6.         newFeed.transform.localScale = new Vector3 (1, 1, 1);
    7.     }
    In my code above, so bascially when I die, every player will instantiate a UI to their own canvas. Right now, when there's only one player in the room, the SendFeed gets called to that one player(me), everything works fine. But if there's 2 players, I get a nullreference on
    Code (CSharp):
    1. newFeed.transform.SetParent (killFeedUI.transform);
    and the the UI gets instantiated infinately. Why is this happening? Everything works fine with one player, why are there problems with multiple?
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    1,913
    Debug this some more. You can attach MD or VS to Unity and run this. Both should stop when the nullreference happens and you can look up what value is null. Then think about it why this might be the case.
    It's a game-logic issue more than a network issue.
     
    Moddwyn16MC likes this.
  3. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    712
    It's difficult to say for certain going on the code supplied but you probably want to restrict the SendFeed() function to only run on the local player object.

    Try adding
    Code (CSharp):
    1.  if(!photonView.isMine) return;
    as the first line of your SendFeed() function. (you may want to change the SendFeed() script to inherit from Photon.PunBehaviour if it doesn't already, otherwise you won't have available the handy cached reference to the PhotonView component.
     
    Moddwyn16MC likes this.