Okay. I am having little problem. When I call a function that is an RPC but I don’t call it over the network. I call it locally, what it should do is disable one canvas, and enable another. It keeps throwing a nullreferenceexception error although BOTH items are assigned in the editor.
Here is script 1 which calls this function
using UnityEngine;
public class Missile : Photon.MonoBehaviour {
Vector3 properRotation;
float speed = 1.1f;
public GameObject target;
bool shouldGo;
void Start()
{
shouldGo = true;
if (PhotonNetwork.player.ID == 1001)
{
properRotation = new Vector3(transform.rotation.x, -90, transform.rotation.z);
transform.eulerAngles = new Vector3(properRotation.x, properRotation.y, properRotation.z);
}
else
{
properRotation = new Vector3(transform.rotation.x, 90, transform.rotation.z);
transform.eulerAngles = new Vector3(properRotation.x, properRotation.y, properRotation.z);
}
}
void Update () {
if (shouldGo)
{
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.deltaTime);
}
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject == target.gameObject)
{
PlayerNetworkController network = new PlayerNetworkController();
network.winGame(); //HERE IS WHERE IT IS CALLED
shouldGo = false;
networkView.RPC("hitPlanet", PhotonTargets.Others, null);
}
}
}
And here is where the function is
using UnityEngine;
public class PlayerNetworkController : Photon.MonoBehaviour {
public Canvas Main;
public Canvas Win;
void Start(){
}
public void lose()
{
photonView.RPC("winGame", PhotonTargets.Others, null);
LeaveRoom();
}
public void LeaveRoom()
{
PhotonNetwork.LeaveRoom();
}
void OnLeftRoom()
{
PhotonNetwork.LoadLevel(Application.loadedLevel);
}
[PunRPC]
public void winGame()
{
Main.GetComponent<Canvas>().enabled = false;
Win.GetComponent<Canvas>().enabled = true;
}
[PunRPC]
void hitPlanet()
{
PhotonNetwork.LeaveRoom();
}
}
In the editor. Both canvases are the child of the prefab the script is attached to that requires them. I have run multiple tests. They are still correctly assigned through out the entire process.
public class Missile : Photon.MonoBehaviour {
[LIST=1]
Vector3 properRotation;
GameObject player;
float speed = 1.1f;
public GameObject target;
bool shouldGo;
[/LIST]
Then when you instanciate your Missile you can add the player who lunch it :
public void LaunchRocketAtPlanet()
{
GameObject missile = PhotonNetwork.Instantiate("Missile", transform.position, Quaternion.identity, 0) as GameObject; //MISSILE IS CREATED HERE
missile.GetComponent<Missile>().target = selectedPlanet;
missile.GetComponent<Missile>().player = myPlanet;
myPlanet.GetComponent<PlayerUnits>().missiles -= 1;
GameManager.isLaunchingMissile = false;
}
Then
void OnCollisionEnter(Collision col)
{
if (col.gameObject == target.gameObject)
{
PlayerNetworkController network = this.player.GetComponent<PlayerNetworkController>();
network.winGame(); //HERE IS WHERE IT IS CALLED
shouldGo = false;
networkView.RPC("hitPlanet", PhotonTargets.Others, null);
}
}
Yep, this is exactly what I did after reading your last post saying it creates a new script of that type therefore leaving both canvases un referenced.