Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by NGEpic · Feb 05, 2017 at 02:55 AM · c#uicanvasfps controllerdeath

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NGEpic · Feb 05, 2017 at 05:59 PM 0
Share

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)"

avatar image CorruptedTNC NGEpic · Feb 06, 2017 at 11:48 PM 1
Share

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

avatar image NGEpic CorruptedTNC · Feb 15, 2017 at 05:08 PM 0
Share

Thanks, I got it working.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

325 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges