• 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
3
Question by p_unity · Mar 17, 2015 at 03:48 PM · uiinstantiatecanvasscreenspace

instantiating elements in UI/Canvas

Hey guys. Basically all i need to do is randomly spawn sprites that fall across the screen on the screen space overlay canvas. Instantiate needs a vector 3 as a position, but the canvas does not have a vector3 position just a rect transform. So either need to instantiate using rect transform (which doesnt seem like it woul dbe possible) or get the equivelent postion in the UI using a vector. Hope that makes sense.

Something like this

 GameObject randomGerm = germs[Random.Range(0, germs.Length)];
 
 Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x) ,spawnValues.y,spawnValues.z);
 
                 Quaternion spawnRotation = Quaternion.identity; //new Quaternion ();
 
                 GameObject germSpawned = Instantiate (randomGerm, spawnPosition, spawnRotation) as GameObject;


But this will only spawn in world space not the UI/Canvas.. hope you know what im aiming at

thanks guys

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

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Wiki

Answer by pfreese · Mar 18, 2015 at 05:28 AM

The important thing is to parent the newly instantiated object to the canvas.

You can also instantiate the object using the single parameter version of Object.Instantiate():

 public static Object Instantiate(Object original);

and then modify the transform of the returned object:

 GameObject germSpawned = Instantiate(randomGerm) as GameObject;
 germSpawned.transform.SetParent(canvas.transform);
 germSpawned.transform.localPosition = spawnPosition;
 germSpawned.transform.localRotation = spawnRotation;
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 p_unity · Mar 18, 2015 at 09:25 AM 1
Share

Hey there.. Well germs is an array of gameObjects (the images have empty gameobjects as parents for animation purposes). Where do i set the parent? before setting the rectTransform components? Also that is still using Vector 3s, as spawnPostiin is a Vector3 and i cant use a vector3 to place a UI image in screen space.

avatar image pfreese · Mar 18, 2015 at 11:06 PM 0
Share

Does the GameObject you are cloning have a plain old Transform or a RectTransform? Either can be used to set the position in the Canvas, but the interface will be slightly different for each. However, you will set the parent the same way:

germSpawned.transform.SetParent(canvasObject.transform);

Using the Transform component, you can set the X,Y values of the position; they should be relative to the center of the canvas. If the game object has a RectTransform (or you replace the transform with a RectTransform using AddComponent), then you can set the anchoredPosition and it will use the behavior as determined by the anchor$$anonymous$$in/anchor$$anonymous$$ax/pivot values.

avatar image zedsmith52 · Jun 01, 2018 at 09:32 AM 0
Share

You could rewrite those 4 lines as:

 GameObject germSpawned = Instantiate(randomGerm, spawnPosition, spawnRotation) as GameObject;
  germSpawned.transform.SetParent(canvas.transform);

but either way works just as well ;)

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Object Instantiate Image to Canvas, Object destroy, Image destroy? 1 Answer

Find UI Image position in Screen Space 1 Answer

Unity UI Position 2 Answers

UI Buttons visible but not clickable when using two canvases. 3 Answers

Strange Canvas behaviour at Screen Space - Camera mode? 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