Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
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 this 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 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 something invisible but still retain references and control over your objects, use the component 'CanvasGroup' on any of your 'Canvas' objects or its children UI nodes.

There are 4 options you can use on this component at this time:

  1. Alpha - set to 1 for full visibility of this element and its children. 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 - this doesn't really work at the time of this post, set it to false and ignore this 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 this 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);
 }


Something like this should work. Make a public GameObject and drag and drop your UI Button in inspector. Then turn the GameObject on and off with SetActive.

This is how I would do it at least.

EDIT* WARNING* This 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);
     }

$$anonymous$$aybe 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 at 01:25 AM

I Know This 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.

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

Changing button positions on different resolutions 1 Answer

Augmented reality - UI - How do I augment an User Interface Canvas properly? 0 Answers

canvas group breaks button 0 Answers

How do i get the UI button position? 2 Answers

Animation script help light problem 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