• 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 /
avatar image
0
Question by Ryujose · Jan 11, 2015 at 03:16 AM · uibuttonplayercanvasondestroy

UI 4.6 canvas button, how to make it invisible on gameplay and how to make it visible when player is dead?

Hello community.

How can I call UI 4.6 canvas button when GameObject is Destroyed?

Before on UI legacy I called like t$$anonymous$$s example on the script "player"

 public void OnDestroy()
     {
             // Game Over
         if (Application.isLoadingLevel)
         {
             DontDestroyOnLoad(gameObject);
         }
         else
         {
             transform.parent.gameObject.AddComponent<GameOverScript>();
         }
          
     }


I want to make those buttons invisible and do it visible when player is dead. But I don't know how I can do it.

I'll appreciate any help.

Regards.

Comment
Add comment · Show 1
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 DavidRoad · Feb 19, 2022 at 01:22 AM 0
Share

I Know I Am Late And I Know This Isn't What Your Looking For, But The Script Is Good

 using System;
 using UnityEngine;
 using System.Collections;
 
 public class PlayerDeath : MonoBehaviour {
 
     void Start()
     {
         //playerdeath
     }
 
     void OnTriggerEnter()
     {
         Application.LoadLevel("DeathScene");               //will load death scene
         Debug.Log("Player Has Been Killed");               //on console it will say Player Has Been Killed
     } 
 }

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by byrne · Jan 13, 2015 at 04:22 AM

Hi, I am currently doing a lot of work on unity uGUI. If you would just like to make somet$$anonymous$$ng invisible but still retain references and control over your objects, use the component 'CanvasGroup' on any of your 'Canvas' objects or its c$$anonymous$$ldren UI nodes.

There are 4 options you can use on t$$anonymous$$s component at t$$anonymous$$s time:

  1. Alpha - set to 1 for full visibility of t$$anonymous$$s element and its c$$anonymous$$ldren. 0 for full invis.

  2. Interactable - set to true if it has buttons that require mouse interaction events.

  3. Block raycasts - set to false if you want the user to be able to click through all nodes.

  4. Ignore Parent Groups - t$$anonymous$$s doesn't really work at the time of t$$anonymous$$s post, set it to false and ignore t$$anonymous$$s option.

Try to set these variables in code in your Start() or Awake() functions or another function, because they have quirky behaviors like resetting their flags after some random action. It has been already reported.

Hope t$$anonymous$$s helps!

Comment
Add comment · 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
0

Answer by DwaynePritchett · Jan 11, 2015 at 04:20 AM

 public class GameOverScript : MonoBehaviour
 public GameObject canvasButton;
 void OnEnable(){
      canvasButton.SetActive(true);
 }
 
 void OnDisable(){
      canvasButton.SetActive(false);
 }


Somet$$anonymous$$ng like t$$anonymous$$s should work. Make a public GameObject and drag and drop your UI Button in inspector. Then turn the GameObject on and off with SetActive.

T$$anonymous$$s is how I would do it at least.

EDIT* WARNING* T$$anonymous$$s code won't work as is. It's pseudo code to help get the point across.

Comment
Add comment · Show 1 · 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 Ryujose · Jan 13, 2015 at 03:11 AM 1
Share

Thanks for the help but it doesn't Works, only a part of it I think.

The error in console is this.

NullReferenceException: Object reference not set to an instance of an object GameOverScript.OnEnable () (at Assets/Scripts/GameOverScript.cs:123) UnityEngine.GameObject:AddComponent() NaveJugador:OnDestroy() (at Assets/Scripts/NaveJugador.cs:244)

And the script modified by me is this.

 public Button buttonRestartLevel;
 public Button buttonBack;
 
 
 
  void OnEnable()
     {
         buttonRestartLevel.gameObject.SetActive(true);
         buttonBack.gameObject.SetActive(true);
         
     }
 
    void OnDisable()
     {
         buttonRestartLevel.gameObject.SetActive(false);
         buttonBack.gameObject.SetActive(false);
     }

Maybe it doesn't Works because gameObject.SetActive doesn't Works for UI canvas?

I've searched on scripting API but there's nothing for disable or enable canvas renderer

http://docs.unity3d.com/ScriptReference/CanvasRenderer.html http://docs.unity3d.com/ScriptReference/Canvas.html

avatar image
0

Answer by DavidRoad · Feb 19, 2022 at 01:25 AM

I Know T$$anonymous$$s Isn't What Your Looking For, But Try It

 using System;
 using UnityEngine;
 using System.Collections;
 
 public class PlayerDeath : MonoBehaviour {
 
     void Start()
     {
         //playerdeath
     }
 
     void OnTriggerEnter()
     {
         Application.LoadLevel("DeathScene");               //will load death scene
         Debug.Log("Player Has Been Killed");               //on console it will say Player Has Been Killed
     } 
 }
Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

Buttons in UI Scroll Rect 2 Answers

block clicking gameobject behind ui 1 Answer

How to make World Space Canvas be on top of Overlay Canvas? 0 Answers

how I can hide a canvas in Unity and shows it after some days using C# Script ? 1 Answer

I Have a Button in Canvas, i Click anywhere except the button and it gets clicked 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