PUN RPC , Object reference not set to an instance of an object.
Options
Hi everybody! First, sorry for my bad englsu ^^ I'm french!
I use unity 5.6, android , and it's a 2D game
I'm trying to use RPC to send to other player that my player have his weapon's sprite changed,
this method i call to my "equipment script" in a object wich is not in my player object
my weapon sprite is in a object "weapon" , in my player object ! but unity said ;
NullReferenceException: Object reference not set to an instance of an object
PunRPCmethod.ChangeWeapon () (at Assets/PunRPCmethod.cs:38)
Here my script attach to my player :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PunRPCmethod : Photon.MonoBehaviour {
public ItemDB database;
PhotonView playerview;
SpriteRenderer weapon;
public int ItemID;
// Use this for initialization
void Start () {
database = GameObject.FindGameObjectWithTag("Item Database").GetComponent();
}
// Update is called once per frame
void Update () {
}
public void ChangeweaponRPC()
{
if (photonView.isMine)
{
this.photonView.RPC("ChangeWeapon", PhotonTargets.AllBuffered, null);
}
}
[PunRPC]
public void ChangeWeapon()
{
int ItemID = GameObject.FindGameObjectWithTag("WeaponSlot").GetComponent().itemID;
SpriteRenderer weapon = GameObject.FindGameObjectWithTag("MainWeapon").transform.GetChild(0).gameObject.GetComponent();
weapon.sprite = database.Items[ItemID].ItemIcon;
Debug.Log("Change weapon" + GameObject.FindGameObjectWithTag("WeaponSlot").GetComponent().itemID);
}
}
Thx for reading me !
Have a good day !
I use unity 5.6, android , and it's a 2D game
I'm trying to use RPC to send to other player that my player have his weapon's sprite changed,
this method i call to my "equipment script" in a object wich is not in my player object
my weapon sprite is in a object "weapon" , in my player object ! but unity said ;
NullReferenceException: Object reference not set to an instance of an object
PunRPCmethod.ChangeWeapon () (at Assets/PunRPCmethod.cs:38)
Here my script attach to my player :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PunRPCmethod : Photon.MonoBehaviour {
public ItemDB database;
PhotonView playerview;
SpriteRenderer weapon;
public int ItemID;
// Use this for initialization
void Start () {
database = GameObject.FindGameObjectWithTag("Item Database").GetComponent();
}
// Update is called once per frame
void Update () {
}
public void ChangeweaponRPC()
{
if (photonView.isMine)
{
this.photonView.RPC("ChangeWeapon", PhotonTargets.AllBuffered, null);
}
}
[PunRPC]
public void ChangeWeapon()
{
int ItemID = GameObject.FindGameObjectWithTag("WeaponSlot").GetComponent().itemID;
SpriteRenderer weapon = GameObject.FindGameObjectWithTag("MainWeapon").transform.GetChild(0).gameObject.GetComponent();
weapon.sprite = database.Items[ItemID].ItemIcon;
Debug.Log("Change weapon" + GameObject.FindGameObjectWithTag("WeaponSlot").GetComponent().itemID);
}
}
Thx for reading me !
Have a good day !
0
Comments
-
EDIT :
i change method , i have no error message, but my sprite doesn't change on other client
public void ChangeweaponRPC()
{
if (photonView.isMine)
{
this.photonView.RPC("ChangeWeapon", PhotonTargets.AllBuffered, null);
}
}
[PunRPC]
public void ChangeWeapon()
{
SpriteRenderer weapon = GameObject.FindGameObjectWithTag("MainWeapon").transform.GetChild(0).gameObject.GetComponent();
weapon.sprite = database.Items[ItemID].ItemIcon;
}
_____________________________________
And in my inventory script :
GameObject.FindGameObjectWithTag("Player").GetComponent().ItemID = itemID;
GameObject.FindGameObjectWithTag("Player").GetComponent().ChangeweaponRPC();
0