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 /
  • Help Room /
avatar image
0
Question by Willexwun · Apr 03, 2020 at 07:39 PM · gameobjectparent transform

Gameobject not instantiating at parent local?

Hello, it's me! I am having a few problems with chunks of my world generating on top of eachother, even though the chunks are apart.

Firstly here is the current code using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class WorldGenerator : MonoBehaviour
 {
     public int sizeX;
     public int sizeZ;
 
     public int groundHeight;
     public float terDetail;
     public float terHeight;
 
     int seed;
 
     public GameObject[] blocks;
     public GameObject Biome;
     public GameObject Chunk;
 
 
     void Start()
     {
         seed = Random.Range(100, 9999);
         //terDetail = Random.Range(11, 18);
         GenerateTerrain();
     }
 
 
     void GenerateTerrain()
     {
         for (int x = 0; x < sizeX; x++)
         {
             for (int z = 0; z < sizeZ; z++)
             {
                 int maxY = (int)(Mathf.PerlinNoise((x / 2 + seed) / terDetail, (z / 2 + seed) / terDetail) * terHeight);
                 maxY += groundHeight;
 
                 GameObject grass = Instantiate(blocks[0], new Vector3(x, maxY, z), Quaternion.identity) as GameObject;
                 grass.transform.SetParent(Biome.transform);
 
                 for (int y = 0; y < maxY; y++)
                 {
                     int dirtLayers = Random.Range(3, 11);
 
                     if (y >= maxY - dirtLayers)
                     {
                         GameObject dirt = Instantiate(blocks[1], new Vector3(x, y, z), Quaternion.identity) as GameObject;
                         dirt.transform.SetParent(Biome.transform);
                     }
 
                     else
                     {
                         GameObject stone = Instantiate(blocks[2], new Vector3(x, y, z), Quaternion.identity) as GameObject;
                         stone.transform.SetParent(Biome.transform);
                     }
 
                 }
             }
         }
 
     }
 }    
 

Following that, I tried the Following to get it to instantiate :

 Biome.transform.SetParent(Chunk.transform);
 //Biome is where the gameobjects I instantiate goes
 
 Biome.transform.localPosition = new Vector3(0, 0, 0);
 //Chunk is the location of which these should be initiated


I have tried much, but still have no idea? Anyone able to assist? Thanks <3

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
0
Best Answer

Answer by streeetwalker · Apr 03, 2020 at 07:59 PM

Hi @Willexwun, I think it is the order of operations that is causing the problem. When you try to instantiate at a position, and then parent, the coordinates can change depending on the scale and position of the parent. I believe you are better off if you just instantiate anywhere, and then set the object's parent, and then use localPosition to set it's position relative to the parent. I think if you use that order, it will work.

*** Well, after our conversations, yes the above is the problem (the rationale is wrong, but the order of operations is correct) - because you instantiate at the same World x and z position, you cannot set the local position directly in the Instantiate statement. You have to do that after you instantiate, either that or add the parent offset to the instantiate position parameter. See the following code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BiomeMaker : MonoBehaviour {
     
     public int sizeX;
     public int sizeZ;
 
     public int groundHeight;
     public float terDetail;
     public float terHeight;
 
     int seed;
 
     public GameObject[] blocks;
     public GameObject Chunk;
 
 
     void Start() {
         for( int i = 0; i < 2; i++ ) {
             seed = Random.Range( 100, 9999 );
             GameObject biome = new GameObject( "biome_" + i );
             biome.transform.position = new Vector3( sizeX * blocks[0].transform.localScale.x * i, 0, 0);
             biome.transform.SetParent( Chunk.transform );
             GenerateTerrain( biome );
         }
     }
 
     void GenerateTerrain( GameObject biome ) {
         // the problem was you were still generating the blocks for
         //  each biome at the same world x and z positions
         //  instead you need to add this offset to each block x
         float offsetX = biome.transform.position.x; 
 
         for( int x = 0; x < sizeX; x++ ) {
             for( int z = 0; z < sizeZ; z++ ) {
                 int maxY = (int) ( Mathf.PerlinNoise( ( x / 2 + seed ) / terDetail, ( z / 2 + seed ) / terDetail ) * terHeight );
                 maxY += groundHeight;
 
                 GameObject grass = Instantiate( blocks[0], new Vector3( x + offsetX, maxY, z ), Quaternion.identity ) as GameObject;
                 grass.transform.SetParent( biome.transform );
                 
                 for( int y = 0; y < maxY; y++ ) {
                     int dirtLayers = Random.Range( 3, 11 );
 
                     if( y >= maxY - dirtLayers ) {
                         GameObject dirt = Instantiate( blocks[1], new Vector3( x + offsetX, y, z ), Quaternion.identity ) as GameObject;
                         dirt.transform.SetParent( biome.transform );
                     } else {
                         GameObject stone = Instantiate( blocks[2], new Vector3( x + offsetX, y, z ), Quaternion.identity ) as GameObject;
                         stone.transform.SetParent( biome.transform );
                     }
                 }
             }
         }
     }
 }


Comment
Add comment · Show 9 · 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 Willexwun · Apr 03, 2020 at 09:50 PM 0
Share

@streeetwalker So I did some re-modeling of the code (sort of) and this is what I came up with.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WorldGenerator : $$anonymous$$onoBehaviour
 {
     public int sizeX;
     public int sizeZ;
 
     public int groundHeight;
     public float terDetail;
     public float terHeight;
 
     int seed;
 
     public GameObject[] blocks;
     public GameObject Biome;
     public GameObject Chunk;
 
 
     void Start()
     {
         seed = Random.Range(100, 9999);
         //terDetail = Random.Range(11, 18);
         GenerateTerrain();
     }
 
 
     void GenerateTerrain()
     {
         Biome.transform.SetParent(Chunk.transform);
         Biome.transform.localPosition = new Vector3(0, 0, 0);
 
         for (int x = 0; x < sizeX; x++)
         {
             for (int z = 0; z < sizeZ; z++)
             {
                 int maxY = (int)($$anonymous$$athf.PerlinNoise((x / 2 + seed) / terDetail, (z / 2 + seed) / terDetail) * terHeight);
                 maxY += groundHeight;
 
                 GameObject grass = Instantiate(blocks[0], new Vector3(x, maxY, z), Quaternion.identity) as GameObject;
                 grass.transform.SetParent(Biome.transform);
 
                 for (int y = 0; y < maxY; y++)
                 {
                     int dirtLayers = Random.Range(3, 11);
 
                     if (y >= maxY - dirtLayers)
                     {
                         GameObject dirt = Instantiate(blocks[1], new Vector3(x, y, z), Quaternion.identity) as GameObject;
                         dirt.transform.SetParent(Biome.transform);
                     }
 
                     else
                     {
                         GameObject stone = Instantiate(blocks[2], new Vector3(x, y, z), Quaternion.identity) as GameObject;
                         stone.transform.SetParent(Biome.transform);
                     }
 
                 }
             }
         }
 
     }
 }    
 
 

And it still appears like this - alt text

Green is grass, as the two overlay. Any advice? Also, thanks for the quick response.

diib.png (67.0 kB)
avatar image streeetwalker Willexwun · Apr 04, 2020 at 05:27 AM 0
Share

I took your code, and I can't get it to mess up - it works perfectly every time, and the grass is always on top. You must have something else going on. Are not generating multiple biomes in the same position with different ground heights?

avatar image Willexwun streeetwalker · Apr 04, 2020 at 12:21 PM 0
Share

@streeetwalker The plan is to generate two biomes, [same code] at two different locations. It will always instantiate both biomes at (0,0,0). It works for one, but not for more than one. Current order in Hierarchy (So you have another look)

Chunk (0,0,0) -Biome -Cave

Chunk2 (24, 0, 0) -Biome -Cave

The cave has the same [transform.localposition || transform.setparent] code that the biome has, and the cave moves to the chunk location.

Show more comments

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

271 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 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 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 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

How to set parent to components object? 1 Answer

is there simple way to put random sprite to gameobject multi child with no repating. 1 Answer

Animation won't stop (Solved) 2 Answers

Unity 5 NullReferenceException after loading new scene. 1 Answer

gameobject deforms when rotation changes 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