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 /
avatar image
0
Question by eni_cay · Dec 11, 2019 at 08:14 PM · uiprefabsruntimeassign

assign ui panel to a prefab object ?

i have a ball object that keeps spawning , and when a player loses the game displays a game over panel , the problem here is that i couldn't assign the panel to the ball (ball prefab) , so how do i make it possible ? ps : the ball spawn endlessly , i tested the panel without making the ball a prefab and it worked just fine but the ball spawns just once and when i made the ball a prefab the panel wouldn't show when the player loses

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

2 Replies

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

Answer by WheresMommy · Dec 12, 2019 at 05:30 AM

You need to fire an event if the player loses. Then have the ui panel listen for that event and if triggered show the panel.

Edit: just to clarify, a prefab is loaded while the ui panel is on scene. Therefore you cant hold a reference to the panel if you instantiate the ball. You could findobjectpftype or do something but that would be some hacky way of doing it. I would go with a manager or a message broadcast to keep things object oriented and not stitched together.

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 eni_cay · Dec 12, 2019 at 09:37 AM 0
Share

i hope im not asking for a lot but can u provide an example ? and thank you

avatar image WheresMommy eni_cay · Dec 12, 2019 at 12:53 PM 0
Share

I'd go with a Game (or UI) $$anonymous$$anager, that holds a static instance of itself. Then you could either use delegates (thats another story) or just call that instance of the manager to call the game over function. Here is an untested code snippet

 public class Game$$anonymous$$anager : $$anonymous$$onoBehaviour
 {
     public GameObject yourPanel;
 
     public static Game$$anonymous$$anager instance;
 
     private void Awake()
     {
         //Check if instance already exists
         if (instance == null)
             instance = this;
         else if (instance != this)
             Destroy(gameObject);
 
         DontDestroyOnLoad(gameObject);
     }
 
     public void DoSomethingOnGameOver()
     {
         //Your script, like yourPanel.SetActive(true);
     }
 }
 
 public class YourBallPrefabScript : $$anonymous$$onoBehaviour
 {
     //Use on destroy or oncollisionenter or whatever you prefer to run the code
     public void YourFunction()
     {
         Game$$anonymous$$anager.instance.DoSomethingOnGameOver();
     }
 }
avatar image eni_cay WheresMommy · Dec 12, 2019 at 02:02 PM 0
Share

update :so far so good , although currently the panel is showing for just few seconds and then disappears , i'll try and see my way , but if u got any suggestions feel free to drop em , and thanks a bunch !

avatar image
0

Answer by hameed-ullah-jan · Dec 12, 2019 at 10:10 AM

The easiest way would be to find the panel in the start function of your ball prefab, though it is not the best way there are many other ways to do it, but for you it will work. let me write some code snippet for you:

this script should be on your ball prefab "ball_script.cs", inside this script write this code:

 public GameObject GameOverPanel;
 
 void Start()
 {
       GameOverPanel = GameObject.Find("YourPanelName");
 }

  
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 eni_cay · Dec 12, 2019 at 10:24 AM 0
Share

um it basically created a prefab of the panel , so im having same results , thank you

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

202 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

Related Questions

Tracking instantiated objects 1 Answer

Remove object assignment in runtime? 1 Answer

How can I assign a GameObject to a variable at runtime? 0 Answers

Children of prefab disappear in Hierarchy when prefab parent is clicked,Children of prefab disappear in Hierarchy when prefab is clicked 2 Answers

Very, very strange float assignment problem. 2 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