Need help with death screen
Hi, thanks for your interest.
So I'm currently building a survival game in Unity 5 and I already made sliders in a canvas etc to track the values of a player's vitals like health hunger etc...
I have a C# script that currently controls the player vitals like checking if they reach zero, adding values to them, etc...
In one of the functions on that script I have an OnDeath void that basically sets the FPSController's walk values and stuff to 0 so it basically disables the player in general. What I'm aiming to add to the function was a part where on death a death UI (That I've already created and set to be disabled) to become enabled so that the player knows they've died and is able to respawn. I put the canvas which holds all the UI parts into the FPSController game object but when I added "DeadCanvas.isActive = true;" to my death function it just errors out and says DeadCanvas doesn't exist even though I already created it and the names match.
I think the issue is that I need to somehow access the DeadCanvas portion of my project inside of the PlayerVitals script, but I'm unaware of how to do that. If possible, please explain that to me and it would be greatly appreciated.
If you need screenshots or anything of that nature feel free to ask.
Answer by CorruptedTNC · Feb 05, 2017 at 03:02 PM
If the Death UI already exists in your canvas, you can do two things:
You could save a reference to the death UI through a GameObject reference. IsActive has been deprecated (retired, not in use anymore) for quite some time, use SetActive(true) instead.
public GameObject DeathCanvas;
void OnDeath()
{
DeathCanvas.SetActive(true);
}
Or you could have a UI manager script. Often you'll find that your project is becoming quite large and holding all of the different logic for your UI causes your scripts to become large and hard to navigate. This becomes the case when you have lots of buttons to click, things to track etc.
However this isn't much of an option until you've grasped the concept of GetComponent Lets say that you have a script on the main camera for your user interface, and another script on your player to keep track of their health, hunger etc.
On your camera script, you might have something like this:
public class UserInterfaceManager : MonoBehaviour
{
public GameObject playerObject;
private PlayerScript player;
void Awake()
{
playerObject.GetComponent<PlayerScript>();
}
Debug.Log(player.Health);
}
You would then drag the player from your scene on to that script and it would then find the script for you. Because we've used GetComponent we can access all of the properties of our player and display the interface accordingly.
I did the first option you suggested, my DeadCanvas is part of my Canvas and I followed what you said to add, but when it gets to the point when that event is supposed to fire it gives me an error "NullReferenceException: Object reference not set to an instance of an object PlayerVitals.Update () (at Assets/PlayerVitals.cs:108)"
When you create a public reference to something, you'll find that in your inspector there's an empty field, like this: http://i.imgur.com/8DozEfT.png
A NullReferenceException
tells you that your script was trying to find something that doesn't exist (in other words, it's equal to null) and therefore it doesn't know what to do and can't continue. Notice how in this picture (and I'm guessing in your inspector) the DeathCanvas reference says "None (Game Object)".
To solve this, you can either drag your death canvas to the right part of your script, or click the little circle to the right of "None (Game Object)" and select it from the list.
You might be slightly confused about what public GameObject DeathCanvas
actually means. DeathCanvas in this case is the name of a variable, which won't automatically find your canvas in your scene. It's just a way of telling your script "Hey I have this GameObject, any time I type DeathCanvas in my code I want you to do something to it.", but you still have to tell your script which GameObject it should be messing with.
Once you're set, it should look something like this: http://i.imgur.com/0wgrduR.png
Your answer
Follow this Question
Related Questions
Canvas Structure for Different UI Panels 0 Answers
cannot find UI GameObject by script 1 Answer
Player Can't Interact With UI 3 Answers
SetActive(false) in Start does not seem to work 2 Answers
Updated with UI text element not correct (same frame) 0 Answers