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
2
Question by ChunkyLow · Mar 13, 2018 at 05:43 AM · transformbeginnerparentinstantiate prefabparent transform

Object not Instantiating Correctly

Hi there,

I'm trying to instantiate an object relative to a prefab's position. So within the script attached to the prefab, I setup the function below. Problem is that the instantiated prefab is not being instantiated at the right position.

I've used a similar script for another prefab and it's working fine. Anyone have any idea why this is the case?

 void CreateEnemySpeech()
 {
     //instantiate speech
     GameObject instEnemySpeech = Instantiate (EnemySpeech, new Vector3(transform.localPosition.x, transform.position.y+200, transform.localPosition.z), transform.rotation) as GameObject;
     instEnemySpeech.transform.SetParent (BattleCanvas, false);
     EnemySpeechText = EnemySpeech.GetComponent<EnemySpeechContainer> ();
     EnemySpeechText.EnemySpeech.text = "text";
     Destroy (instEnemySpeech, 4f);
 }
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
3
Best Answer

Answer by tormentoarmagedoom · Mar 13, 2018 at 11:22 AM

Good day.

I reccomend you to replace this

 instEnemySpeech.transform.SetParent (BattleCanvas, false);

for this:

 instEnemySpeech.transform.SetParent(BattleCanvas);

and if the position where it apears is not the correctone,m change it via scripitng

 instEnemySpeech.transform.position = WhereYouWantPosition ;

Making an object parent/child of another sometimes is complex, becoause if parents have diferent scales and rotations is hard to think each time which should be the correctones... For my experience, is better to first become child/parent, and then move/rotate/scale the objects

 If helped, accept the answer! :D

Bye :D

Comment
Add comment · Show 4 · 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 SohailBukhari · Mar 13, 2018 at 11:31 AM 0
Share

You are right. The object should be instantiating wit respect to his parent so, make your object free and then instantiate. No need to set position after instantiating gameobjct on given position as

 instEnemySpeech.transform.SetParent(BattleCanvas);

I recommend use this way of instantiating.

 public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);

For more information visit unity documentation of instantiating https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

avatar image ChunkyLow · Mar 13, 2018 at 03:09 PM 0
Share

Hey there, thanks for that!

I tried it out, but doesn't seem to work. However, it seems like the issue may be because the prefab has a recttransform property on it and I read that recttransform doesn't work the same as transform.

Any idea how to instantiate a recttransform at a location?

Thanks!

avatar image SohailBukhari ChunkyLow · Mar 13, 2018 at 03:53 PM 0
Share

You should use anchoredPosition to set the position of UI, not transform.position in case of recttransform.

Read The Documentation : https://docs.unity3d.com/ScriptReference/RectTransform-anchoredPosition.html

avatar image ChunkyLow SohailBukhari · Mar 13, 2018 at 03:56 PM 0
Share

Thank you good sir!

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

84 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

Related Questions

Problem assigning transform variable to prefab. 1 Answer

Move Overlapbox/GameObject depending on Parent 2 Answers

Silly Instantiation Issue 0 Answers

Parent object is getting generated why ?? 1 Answer

Accessing children of instances vs children of original prefab 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