Failed attempt to set up UI in the Advanced Tutorial (multiplayer shooter)
I've been trying to set up some UI in the Advanced Tutorial. I want to display a public Text mesh that would show how many times each player gets shot. This is a youtube tutorial I've tried to use: https://www.youtube.com/watch?v=fc3hXRTptjQ&list=PL8OCpfy38RwnwU9iJkV39M42mTXvqj6wt&index=12
So basically in the Level2 scene I've set up a Canvas with a TextMeshProUI for the hit(getting shot) count. Canvas has this script attached to it:
Here I have my ScoreEvent(I know it's technically not a score, but I left the name after following the youtube tutorial): https://ibb.co/PjyWSwn
I've created a new PlayerDataForDisplay script for the data about players that I want to be shown in the screen:
Then in the TutorialWeaponRifle script at the end of if (Physics.Raycast(r, out rh) && impactPrefab) statement I added this piece of code (playerDataForDisplay is initialized like so: private PlayerDataForDisplay playerDataForDisplay;):
Also the PlayerDataForDisplay script is attached to the player as well.
Then I build and run the Level2. Everything is fine until I shoot another player(both from server and from client view). Everything stops and this is what's showing: https://ibb.co/Jx2BYds
The editor log throws a NullReferenceException at the part of the code I added to TutorialWeaponRifle. I guess it's expected but I'm not sure how to proceed. Help is much appreciated!
So basically in the Level2 scene I've set up a Canvas with a TextMeshProUI for the hit(getting shot) count. Canvas has this script attached to it:
using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; public class GameCanvas : Bolt.GlobalEventListener { public TextMeshProUGUI scorePopup; public override void OnEvent(ScoreEvent evnt) { scorePopup.text = evnt.Message; scorePopup.gameObject.SetActive(true); } }
Here I have my ScoreEvent(I know it's technically not a score, but I left the name after following the youtube tutorial): https://ibb.co/PjyWSwn
I've created a new PlayerDataForDisplay script for the data about players that I want to be shown in the screen:
public class PlayerDataForDisplay : Bolt.EntityBehaviour<ITutorialPlayerState> { public int hitsTaken = 0; public void ChangeHitsTaken() { hitsTaken++; var scoreEvent = ScoreEvent.Create(); scoreEvent.Message = PlayerPrefs.GetString("username") + " got hit " + hitsTaken + " times"; scoreEvent.Send(); } }
Then in the TutorialWeaponRifle script at the end of if (Physics.Raycast(r, out rh) && impactPrefab) statement I added this piece of code (playerDataForDisplay is initialized like so: private PlayerDataForDisplay playerDataForDisplay;):
if (rh.collider.CompareTag("Player")) { playerDataForDisplay.ChangeHitsTaken(); }I added the "Player" tag and a collider to the player(just in case I added those to the mesh as well).
Also the PlayerDataForDisplay script is attached to the player as well.
Then I build and run the Level2. Everything is fine until I shoot another player(both from server and from client view). Everything stops and this is what's showing: https://ibb.co/Jx2BYds
The editor log throws a NullReferenceException at the part of the code I added to TutorialWeaponRifle. I guess it's expected but I'm not sure how to proceed. Help is much appreciated!
0