Can't set a players hat sprite for other players using rpc

In the game I'm making I'm trying to allow the player to change their hats in the game.

I'm trying to do this with an rpc.
The rpc takes input of an int which tells it which index of the hat array all players have in their script to pick.
This int x is then used in the rpc by:

hat.sprite = Instantiate(hatSprites[indexs[x]]) as Sprite;
hatTransform.localScale = new Vector2(1, 1);
hatTransform.localPosition = new Vector3(0, 0, -1);

this is meant to set the child object of the player prefab to the hat sprite from the array, however no matter how I try it never works, I have even tried using the sprite renderer of the main player object but it still does not work.
I know the rpc is working cus I have tested and am able to disable the character whos hat needs changing animator which syncs across the players and its child avatar sprite.

i always get the message :
NullReferenceException: Object reference not set to an instance of an object
at CharacterController2D.spriteTransform (System.Int32 s1) [0x00092] in C:\Users\Fraser\Unity Projects\Hidden\Assets\Scripts\CharacterController2D.cs:196

the 196 line being: hat.sprite = Instantiate(hatSprites[indexs[x]]) as Sprite;

any advice please would be very helpful been stuck on this for a couple days, let me know if u need to see anymore code, the rpc is called by:

myPV.RPC("spriteHat", RpcTarget.All, s1); (s1 is the int of the hat index).

Comments

  • Also the hat is set and works fine for the local player
    its just for the other players it is not set.
  • the has is a spriterenderer that is passed into the main object via a serializedFeild from its child hat object same with hatTransform
  • I have since solved this issue, ignore this post unless u have a similar issue then @ me and i may be able to help u :)
  • I may Have the same problem

    I'm creating a multiplayer card Game And trying to instatiate a card with the same cardprefab for all of the cards. I'm trying to update the cardprefab Sprite using PV.RPC but I get bunch of weird errors



    newcardA = PhotonNetwork.Instantiate(cardprefab.name, transform.position, Quaternion.identity);
    PhotonView PV = newcardA.GetComponent<PhotonView>();
    newcardA = Obj; //Obj is a Public GameObject
    PV.RPC("UpdatePrefab", RpcTarget.All, new object[] { P[0].GetNum(),P[0].GetSuit() });


    public void UpdatePrefab(int num,string suit)
    {
    card c = new card();
    c.Create(suit, num, DeckFace); //Generate a Card
    Obj.GetComponent<PlayerScript>().cardface = c.Getface();
    }


  • if it helps U to understand my problem - in the host screen the cards are Instantiate Ok but on the other screens all of the cards get the same Sprite(although its not the Sprite That was first created for the card)
  • sorry @IlayTheBoneless i have only just seen this comment, your problem is different to mine as I was instantiating a sprite to a child component sprite renderer of my player prefab not instantiating using the photon network instantiate. though if you added a child object to the prefab i suppose it would be able to work how I did it.
    if you haven't solved this issue yet let me know and ill explain how I did this, I'm back from my holidays so will reply much faster. ( my solution only works for instantiating only a sprite nothing else)

    hope u have already solved it though :)