I’m currently making a dueling top down RPG like game. I made a basic controller for a character, but after trying to get it to work for multiplayer I kept getting the standalone clients crashing. I have narrowed it down to this part of the script for being the issue.
It plays for a small amount of time (0-30ish seconds) then crashes the standalone client.
if (pv.isMine)
{
GameObject t1 = GameObject.Find("Hp"); //finds texts objects
GameObject t2 = GameObject.Find("Armor");
GameObject t3 = GameObject.Find("A1");
GameObject t4 = GameObject.Find("A2");
GameObject t5 = GameObject.Find("A3");
hpT = t1.GetComponent<Text>(); //gets the text components so we can use them
armorT = t2.GetComponent<Text>();
a1T = t3.GetComponent<Text>();
a2T = t4.GetComponent<Text>();
a3T = t5.GetComponent<Text>();
PhotonNetwork.Instantiate(fireballMainS, ray.transform.position, ray.transform.rotation, 0); //instantiates fireball
fireballMain = GameObject.Find("fireBallMainOnline(Clone)"); //gets the fireball and stores it in a useable value
fireballMain.name = "fireBallMainOnlineEdit" + pv.viewID; //renames fireball so it isnt gotten again by a different player
PhotonNetwork.Instantiate(fireBlastMainS, transform.position, transform.rotation, 0);
fireBlastMain = GameObject.Find("fireExplosionMain 1(Clone)");
fireBlastMain.name = "fireExplosionMainOnlineEdit" + pv.viewID;
}
}
Any help would be appreciated.
When you are using debugger step over, at which line does it crash exactly?
Also, I have to note that approach using GameObject.Find() is rather odd, if not error prone. For best results, you should fetch newly created object in:
void OnPhotonInstantiate(PhotonMessageInfo info)
{
//...
}
of the Photon.MonoBehaviour script attached in the PhotonNetwork.Instantiate(…) object.
That way you will get the reference of the object regardless of the connection on client side.
Is this code called on server or client? What network approach are you using, server-client or peer-to-peer?
1 Like
Thanks so much for the reply,
Debugger step over doesn’t work, as it only crashes on standalone builds but never in the editor.
I knew I was doing that wrong… The code felt very messy but I couldn’t find a simpler solution. I read the docs on OnPhotonInstantiate, from what I read I put it on the Instantiated object, but after that how would I store that in my player controller.
This code is called on all clients, as im using a peer to peer approach.
Well, there are many ways to do it and it depends how your code/logic is structured. For the sake of simplicity, I will assume you have a GameController script that runs your game logic (one per scene), and that your PlayerController inherits from Photon.MonoBehaviour; that it is created by the player via PhotonNetwork.Instantiate() method.
PlayerController could look like this:
using UnityEngine;
using System;
using System.Collections;
public PlayerController : Photon.MonoBehaviour
{
public PhotonPlayer owner;
void OnPhotonInstantiate(PhotonMessageInfo info)
{
this.owner = info.sender;
GameController.Reference.players.Add( this );
}
void OnDestroy()
{
GameController.Reference.players.Remove( this );
}
}
GameController should contain references to all of your player controllers:
using UnityEngine;
using System;
using System.Collections;
public class GameController : MonoBehaviour
{
public static GameController Reference
{
set; get;
}
public List<PlayerController> players = new List<PlayerController>();
void Awake()
{
Reference = this;
}
void Start()
{
// lets say this is where you instantiate your player controller for the sake of example
PhotonNetwork.Instantiate( "PlayerControllerPrefab" );
}
public GetPlayer(PhotonPlayer p)
{
foreach (PlayerController controller in players)
{
if (controller.owner == p)
return controller;
}
return null; //provided photonPlayer has no playerController
}
}
Now, whenever you call your other scripts it is fairly easy to get reference of the owner player:
public class MySpellExample : Photon.MonoBehaviour
{
void OnPhotonInstantiate(PhotonMessageInfo info)
{
PlayerController player = GameController.Reference.GetPlayer(info.sender);
if (player == null)
{
Debug.LogError("Player not found for PhotonPlayer :"+info.sender.ID);
return;
}
player.awesomeSpells.Add( gameObject ); //<- add your object here
}
}
Please note that I haven’t done peer-to-peer games with PhotonNetwork so I can’t suggest the best way to do it.
Also, for the random crash, have you tried out commenting out certain parts of the code to narrow down the suspect even more? I would highly suggest you replace your GameObject.Find(“HP”) and similar scripts by referencing them directly via inspector to a public variable. Best of luck!
1 Like
I changed it up a bit, and it worked! I redid some of the other code too, and it works, no more crashes. 
Thanks alot @Fajlworks !
1 Like