• 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 ToasterPas · Jun 04, 2015 at 02:12 PM · instantiate prefab

How to set a specific location of a Prefab within the child of a Canvas?

I'm looking for a way to Instantiate a Prefab at a specific location within a Panel. Here are the specific details:

  • The Panel is using anchors at X(0, 0.8) and Y(0.4, 0.8), so the size of the Panel will change as the screen size changes.

  • The Prefab is a Transform with one Child that contains a Transform and a Sprite Renderer.

  • The Canvas 'Render Mode' is set to 'Screen Space - Overlay'.

  • I'm trying to do this with a number of different Prefabs of various sizes.

  • The Scale on everything in the Scene (as far as I can tell) is (1, 1, 1).

  • I've been using: thingy = (Transform)Instantiate(PrefabThingy, new Vector3(0, 0, 0), Quaternion.identity); to Instantiate the Prefab, then setting the thingy.transform.position to a different Vector3, outlined below.

I've tried a couple of things without success. If I don't set the Prefab's parent to the Panel, it ends up in the middle of the screen with Transform position (0, 0, 0). If I set the Prefab's parent to the Panel, it ends up at (0, -385.6, 0). Setting the Y value to 0 puts the Prefab way off-screen.

When I move a larger Prefab to (1, 0, 0), it hardly moves. When I move a smaller Prefab to (1, 0, 0), it moves a great distance. This is happening even when the Prefabs both have the same Panel parent. Both Prefabs have Sprites with the same Pixels Per Unit.

I'm confused as to where (0, 0, 0) is relative to each of these GameObjects.
I'm also not sure why setting two objects to (1, 0, 0) seems to put them in different places, even within the same Parent. How is this related to the size of the Prefabs?

I would like to know how to set the Prefabs both at some relative distance from the edges of their parent Panel (say, 20% from the left edge) and how to set some absolute distance from the edges of their parent Panel (say 50 pixels from the top).

Any help would be greatly appreciated!

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Problem instantiating a prefab, and storing a reference to it. 1 Answer

How do I spawn a random unit from my XML file? 0 Answers

aim instantiated GUI text at camera 0 Answers

When attempting to place an object from prefab, the object continually jumps up to the camera and back down quickly. 0 Answers

Argument Exception: The Object you want to instantiate is null (A new one?) 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