Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 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 Ollie_Graham · Jul 28, 2015 at 10:24 AM · 2duiprefabbutton

Enable UI Button from Prefab

Hey guys, I have the problem that I cannot set my Button vars from my script to the appropriate UI buttons.

What I have going on is that when my "Player" prefab dies, it destroys the game object and makes two buttons appear by enabling them. In my code I am using the UnityEngine.UI and the vars for the buttons are of type button, however when I go into the editor to drag and drop the buttons from the hierarchy into the 'None (Button)' slots under the script, it does not allow me to do so.

But it does allow me to drag and drop in the UI buttons correctly if I drag an instance of the player prefab into my scene and then try and do it. (But it does not apply this back onto the prefab itself if I hit apply.)

Can anyone tell my why this is happening/ how to fix it? Thanks. Here's the code:

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class CollisionDetection : MonoBehaviour {

 public bool IsGameOver;
 public int Health;

 public Button resetButton;
 public Button menuButton;

 public GameObject BloodParticles;



 void Awake (){
     IsGameOver = false;
     Health = 10;

 }
 

 void OnCollisionEnter2D (Collision2D col)
 {

     if(col.gameObject.name == "Enemy 1 - Buzzsaw(Clone)")
     {
         Health -= 1;
         Debug.Log ("Health is " + Health);
     }

     if(col.gameObject.name == "Enemy 3 - Drone(Clone)")
     {
         Health -= 1;
         Debug.Log ("Health is " + Health);
     }

     if(col.gameObject.name == "BulletPrefab(Clone)")
     {
         Health -= 1;
         Debug.Log ("Health is " + Health);
     }
 

     if (Health == 0){
         Debug.Log ("GAME OVER");
         IsGameOver = true; 
         resetButton.gameObject.SetActive (true);
         menuButton.gameObject.SetActive (true);
         Destroy (gameObject);
         GameObject Clone;
         Clone = (Instantiate (BloodParticles, transform.position,transform.rotation))as GameObject;

     
     
     }



     
 }

}

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 AlexGan001 · Jul 28, 2015 at 12:35 PM 0
Share

What buttons are you attempting to drag into the UI Button slots??

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by _dns_ · Jul 28, 2015 at 02:37 PM

Hi, if I understand well, what you are trying to do is referencing an instance in a scene (buttons) from a prefab (player). This is not possible as the prefab "lives" on the disk and doesn't know anything about any scene or instances in it, this is a different environment. This is not easy to explain, imagine that in a scene, each object has an ID that is different each time the scene is loaded. Then, a prefab cannot know what ID an object has in advance. In the contrary, an object in a scene can know the ID of a prefab as it's on the disk and then has a "fixed" filename to reference it (I simplify here, you can read more about this in Unity documentation/tutorial)

So, to do what you want to do: When the player dies, it has to find the UI object to be able to reference them. You can use GameObject.Find to find the UI object by it's name, then enable them. The trick is that gameobject.find does not find disabled/inactive objects.

So what you could do is let the buttons enabled in the scene, then, during the player's Start(), find the buttons by their name, assign them to some private fields of your player class (like a private GameObject MyFirstButton;) and then disable the buttons to hide them before the game begins. Then, on player death, you just have to use MyFirstButton.SetActive to enable them.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to detect button presses and change child object text in the button's Parent Object script 0 Answers

UI Buttons in prefab not working at all 1 Answer

Help check click out of UI 2 Answers

How to make UI buttons so my player can move along certain lines 0 Answers

How to make a button animation in menu 1 Answer


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