Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Nikkimouse · Jul 25, 2015 at 03:56 PM · cameraguigameobjectbutton

Is it possible to link a UI element to a gameobject?

I have three images displayed in my game and my player needs to choose between them (it's a character-selection type of thing). I've built UI buttons for each and they are positioned correctly to be used when testing in the editor, but because they are parented to the Canvas, they shift position and are out of place when I build the game.

I've tried moving the button to parent it to the same empty game object as the sprite in the scene itself, but then the button just disappears and can't be seen at all in either the editor or the game (although it is in the hierarchy still).

Is there a way to force the link to the gameobject/sprites instead? Or is my only option to build a button without the built-in UI system, just with coding?

Comment
Add comment · Show 2
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 Nikkimouse · Aug 13, 2015 at 11:17 AM 0
Share

Okay, so I've gone through all the links that have been given to me but I'm still struggling. When I attempt to instantiate it, the console tells me I cannot set the parent of a prefab object to prevent data corruption and the 'button' is still invisible. If I drag it directly into the canvas in the hierarchy, then it will reappear immediately.

This is my code, where 'redwizard button' is the button I'm trying to keep near the 'redwizard' sprite: (Also note the final position hasn't been decided, I'm just trying to instantiate it at all, which is why the button would currently be appearing on top of the sprite image)

 public Button redwizardbutton;
 public GameObject redwizard;
 
 void CreateWizard () {
     Vector3 redwizardspawnposition = new Vector3 (
         redwizard.transform.position.x,
         redwizard.transform.position.y,
         0);
     Instantiate (redwizardbutton, redwizardspawnposition, Quaternion.identity);
     redwizardbutton.transform.SetParent (redwizard.transform);
 }

Here's the screenshot with the instantiated object selected (the button should say 'Choose' and appear underneath the red wizard).

alt text

Here's a screenshot of where the button actually shows up. alt text

missing-button-selected-in-hierarchy.jpg (301.4 kB)
missing-button-location.png (408.1 kB)
avatar image itsharshdeep · Aug 14, 2015 at 03:59 AM 0
Share

The Quick solution is I think you have to make some a canvas for each like set of the canvas which will instantiate same time when your character instantiated.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by rajavamsidhar_gvs · Aug 10, 2015 at 11:48 AM

hi...i faced this problem.better to instantiate that UI object if you must want built in UI. otherwise just go with default unity scripting.and once take help from google.

 Instantiate(canvas,new vector3 (0,0,0),quaternion.Identity);
Comment
Add comment · Show 10 · 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 Nikkimouse · Aug 10, 2015 at 04:24 PM 0
Share

Thanks! And I guess you'd have to instantiate as a child object as well.

avatar image rajavamsidhar_gvs · Aug 11, 2015 at 04:31 AM 0
Share

yes..you can use " gameobjectname.transform.parent ".

avatar image Nikkimouse · Aug 12, 2015 at 05:55 PM 0
Share

I tried instantiating the UI, but it didn't show up. I think it's because it can't be the child of both the Canvas and the object I want to offset from. How did you manage to get it to be both?

avatar image rajavamsidhar_gvs · Aug 13, 2015 at 05:00 AM 0
Share

sorry for late.and when i'm changing canvas positions or adding it as child to any other object its not work.its better to just use instantiate. and dont go for parent.it will work surly. and can you tell why do you want it as child for a empty gameobject?

avatar image Nikkimouse · Aug 13, 2015 at 05:07 AM 0
Share

Even when I instantiate it, it doesn't show up on screen (although it does in the hierarchy). It has to be a child of the Canvas in order to show up, but I want it to be linked to another 'normal' sprite (or else it will end up being off-position when the player chooses different screen resolutions). How did you get it to actually appear on the screen when instantiated?

Show more comments
avatar image
0

Answer by moriggi · Aug 15, 2015 at 10:07 PM

hi create a canvas, inside it put 3 images of the 3 wizards and position all in the right position and pivot by the RectTransform component. then put as child of canvas the 3 buttons. add an event of the button, drag and drop the Game Object containing the function you need to execute inside the button event and select public void CreateWizard(). when the button is clicked the function is executed. do it for all the 3 buttons

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI Window on GameObject location? 1 Answer

GameObject touchable instead GUI button 3 Answers

Render GameObjects over GUI.Layer 1 Answer

Using GUI and check what button was pressed 1 Answer

resetting the my default value for camera field of view 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