Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Sarthak123 · Nov 04, 2014 at 07:58 AM · instantiateparentingparent transform

Error : Transform.parent not working properly.

I have a script attached to my "Land" GameObject which spawn obstacles at different random co-ordinates, this is the main script.

 var cube : GameObject;
 
 function Start () {
 var newCube = Instantiate(cube, Vector3(Random.Range(-22, 22), 7.5, Random.Range(transform.position.z-4,transform.position.z+4)), Quaternion.identity);
 }


it works properly, but i want to make these obstacles child of their respective "Land" so i added this to the script and finally it becomes,

 var cube : GameObject;
 
 function Start () {
 var newCube = Instantiate(cube, Vector3(Random.Range(-22, 22), 7.5, Random.Range(transform.position.z-4,transform.position.z+4)), Quaternion.identity);
 newCube.transform.parent = gameObject.transform;
 }

but the problem is it starts spawning more than 1 obstacle per land, which is un-intended,

here are the screenshots,

Before adding newCube.transform.parent = gameObject.transform;

After Adding it.

i cant figure out the problem itself, why it duplicates my blocks.

Note :- One Land Object Spawns one Block, and there are 6-7 land blocks in the above images.

Comment
Add comment · Show 12
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 Sarthak123 · Nov 04, 2014 at 08:36 AM 0
Share

please help

avatar image AlwaysSunny · Nov 04, 2014 at 08:39 AM 0
Share

To what object is the script attached? It looks like you're duplicating each successively generated land object rather than instantiating them from an untouched prefab.

avatar image Sarthak123 · Nov 04, 2014 at 09:30 AM 0
Share

actually i am duplicating the land, and from that land creating a obstacle, to create a infinite world

avatar image Sarthak123 · Nov 04, 2014 at 09:31 AM 0
Share

i want to have only one block under each of the land, but its generating more than one

avatar image HarshadK · Nov 04, 2014 at 09:42 AM 0
Share

Can you share your land generation code?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NickP_2 · Nov 04, 2014 at 01:07 PM

Instatiate transforms, not gameobjects :)

So the prefab property 'cube' should be a transform! new code:

  var cube : Transform;
 
  function Start () {
  var newCube = Instantiate(cube, Vector3(Random.Range(-22, 22), 7.5, Random.Range(transform.position.z-4,transform.position.z+4)), Quaternion.identity);
  newCube.parent = gameObject.transform;
  }


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 Sarthak123 · Nov 04, 2014 at 01:15 PM 0
Share

still the same problem bro...:(

avatar image Bunny83 · Nov 04, 2014 at 01:35 PM 0
Share

@NickP_2: That won't change anything. It doesn't matter if you instantiate a GameObject or a component of that gameobject as it will always instantiate the whole gameobject including all components and childs.

However in most cases it's more convinient to use Transform ins$$anonymous$$d of GameObject, but it won't change the behaviour at all, just the return type ;)

avatar image NickP_2 · Nov 04, 2014 at 01:44 PM 0
Share

Strange, I had similar problems and it was because I instantiated gameobjects ins$$anonymous$$d of transforms. Ah too bad.

avatar image Sarthak123 · Nov 04, 2014 at 01:51 PM 0
Share

thanks @Bunny83 you were able to find the exact mistake i was making..

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

29 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

Related Questions

Parenting instantiated particle system 1 Answer

Instantiate an object as a child of the gameobject's parent 2 Answers

[Solved] How to Dynamicaly assemble an Object in your Resource Folder 1 Answer

Problems setting parent of instantiated objects 2 Answers

Photon Cloud parenting instantiated gameobject 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