- Home /
When using photon sometimes it fails to find local game object.
Scenario I am using Photon. I have a room that consists of a 2 levels that the players switch between. A game lobby and an arena where the players battle. In the lobby when the timer reaches 0 the players are all spawned in to the arena and fight. After a score limit is reached by a player an RPC call is sent out and a leaderboard UI element appears with a button that sends the player back to the lobby level to wait for the next match to start.
The Problem For whatever reason clients that are not the master client are having trouble using "GameObject.Find()" to grab the match timer text by name. When everyone initially joins the room the timer seems to work correctly. If players leave the arena before or close to the time the master client does the timer works correctly. However if the client waits 5 or so seconds then leaves the arena and joins the lobby I get an exception saying that the game object doesn't exist. Testing this in Unity I can see that the object is there and enabled in the hierarchy (It is never set to disabled) its just throwing an exception.
Exception NullReferenceException: Object reference not set to an instance of an object LobbyNetworkedInstanceController.UpdateCountdown (System.Int32 currentCountdown) (at Assets/Scripts/LobbyNetworkedInstanceController.cs:80) LobbyNetworkedInstanceController.RPC_UpdateCountDown (System.Int32 currentCountdown) (at Assets/Scripts/LobbyNetworkedInstanceController.cs:74) System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at :0) Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
Relevant Code
public class LobbyNetworkedInstanceController : MonoBehaviour
{
// Start is called before the first frame update
//LobbyMatchCountDown
int currentCount = 15;
public GameObject AI_Name;
private void Awake()
{
//matchStartingText = GameObject.Find("TXT_MatchStartingIn").GetComponent<Text>();
}
void Start()
{
if (this.gameObject.GetComponent<PhotonView>().isMine)
{
if (PhotonNetwork.player.IsMasterClient)
{
InvokeRepeating("CountDown", 1f, 1f); //1s delay, repeat every 1s
}
}
}
// Update is called once per frame
void Update()
{
}
void CountDown()
{
if(currentCount > 0)
{
currentCount--;
try
{
GameObject.Find("TXT_MatchStartingIn").GetComponent<Text>().text = "Match Starting in: " + currentCount.ToString() + "s";
}
catch
{
}
this.GetComponent<PhotonView>().RPC("RPC_UpdateCountDown", PhotonTargets.AllBuffered, currentCount);
if(currentCount < 7 && PhotonNetwork.playerList.Length + GameObject.FindGameObjectsWithTag("AI_NameHolder").Length < 8)
{
GameObject Ai_Name = PhotonNetwork.Instantiate(AI_Name.name,new Vector2(0, 0), Quaternion.identity, 0);
DontDestroyOnLoad(Ai_Name);
}
}
else
{
this.GetComponent<PhotonView>().RPC("RPC_LoadLevel", PhotonTargets.AllBuffered);
}
}
[PunRPC]
public void RPC_LoadLevel()
{
LoadTheLevel();
}
private void LoadTheLevel()
{
PhotonNetwork.LoadLevel("Arena");
}
[PunRPC]
public void RPC_UpdateCountDown(int currentCountdown)
{
UpdateCountdown(currentCountdown);
}
private void UpdateCountdown(int currentCountdown)
{
currentCount = currentCountdown;
GameObject.Find("TXT_MatchStartingIn").GetComponent<Text>().text = "Match Starting in: " + currentCount.ToString() + "s";
if(currentCount <= 0)
{
PhotonNetwork.LoadLevel("Arena");
}
}
}
Any help will be greatly appreciated. Thank you!
Your answer
Follow this Question
Related Questions
Unity Photon Network - Text Mesh Sync 1 Answer
Problems with Photon self-hosted server 0 Answers
Photon Unity Networking Score Issue 1 Answer
Photon Unity Networking - Projectile Syncing problem 0 Answers
Multiple Cars not working 1 Answer