• 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 Frioniel · Oct 02, 2016 at 05:37 AM · instantiate prefab

Having problem with Instatiate GameObject at Canvas

hello I'm making a some 2D button games.

But I have problem with Instatiate Gameobject at Canvas

here's my simple code

public class SpawnPosition : MonoBehaviour { public GameObject BlueRectPrefab; public GameObject BlueRectSP; // Use this for initialization void Start () { Instantiate(BlueRectPrefab, BlueRectSP.transform.position, BlueRectSP.transform.rotation);

 }

}

I just want my prefab to instantiate at canvas

but when i press play button

nth spawn at my play screen

I checked settings but still got what is problem.

https://docs.unity3d.com/Manual/HOWTO-UICreateFromScripting.html

I read this manual

this manual says setting transform.setparent to false can instantiate the prefab at canvas

but i have still no idea how to do it.

How can i solve this problem???

Comment
Add comment · Show 3
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 gjf · Oct 01, 2016 at 02:48 PM 0
Share

please format your code.

avatar image Frioniel gjf · Oct 04, 2016 at 12:00 AM 0
Share

Thank you for reply and sorry for my unkind code. Well.. I'm not familiar to unity community writing system.

To go on.

$$anonymous$$y code is


GameObject BRect;

BRect = Instantiate(BlueRectPrefab, BlueRectSP.transform.position, BlueRectSP.transform.rotation) as GameObject;

BRect.transform.SetParent(BlueRectPrefab.transform, false);


I put this code at void start

and the object should be in canvas

like


main camera

canvas

   object what i want to spawn(inside canvas)


but it works like this


main camera

object(clone)(not in canvas)

canvas

how can i solve this problem?

avatar image Frioniel gjf · Oct 04, 2016 at 10:53 PM 0
Share

oh I solved this problem

setting code

like this:

BRect.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform.GetChild(0), false)

thanks for reply :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Accessing a Function from an Instantiated Object 1 Answer

Instantiated objects, not reacting to lighting 1 Answer

Button not instantiating, no errors 2 Answers

Trying to implement a cone shot in a 2D Game. 1 Answer

instantiate objects within an area 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