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 glehtien · Jul 04, 2016 at 06:18 PM · parent-childparent transformparent and child

Problem with parent gameObject not where it should be after adding childs

I am playing around with sprite chunks in Unity3D for a 2D game.

One chunk has a sprite prefab which contrains a scale, a type and a UnitySprite for displaying the image. In addition each chunk has an array TileSprite[ , ] for the single sprites.

The Start-method for the Chunk looks like this

  transform.position = new Vector3(position.x, position.y, -1);
         transform.parent = parent.transform;
 
         InitializeSpriteMap();
 
         // Init here with default values for rendering!
 
         for (int x = 0; x < ChunkSize; x++)
         {
             for (int y = 0; y < ChunkSize; y++)
             {
                 TileSprite sprite = (Instantiate(tilePrefab) as GameObject).GetComponent<TileSprite>();
                 // todo Take valiue form generated terrain/Chunk/world
                 
                 sprite.Instantiate("Grass", sprites[TileType.Gras], TileType.Gras, new Vector2(x, y), transform);
                 tiles[x, y] = sprite;
             }
         }



The Instantiate-Part for the TileSprite looks like this:

     public void Instantiate(string name, Sprite sprite, TileType type, Vector2 position, Transform parent)
     {
         SetImage(sprite);
         var bounds = tileSprite.bounds;
         transform.localScale = new Vector3(tileSize / bounds.size.x, tileSize / bounds.size.y, 1);
         bounds = tileSprite.bounds;
         tileName = name + position.ToString(); 
         transform.name = tileName;
         this.type = type;
                 
         SetPosition(position*tileSize);
         transform.parent = parent;
 
     }

TileSprite -> SetPosition

    public void SetPosition(Vector2 position)
     {
         this.position = position;
         transform.position = new Vector3(position.x, position.y, -1);
     }



Soo what is the problem... The TileSpirtes are added correctly without a gap between adjacent TileSprites.

alt text

The problem is that the position of the Chunk is vector3(0,0,0) before adding any TileSprites and after added the tiles it is still Vector3(0,0,0) but in the scene the Chunk has moved!!!!!! It is not in Camera (0,0,0) anymore.

alt text

The Camera is placed in Vector3(0,0,0). I am using center and global as setup for Unity-Editor. I want to add more Chunks next to each other later, but I want the one chunk added to be at (0 ,0 ,0)

FYI: When I add more chunks, the Tiles for each chunk are at the same position the position oif the chunk is changing but only in numbers not visible....

Can anyone explain to me why this is happening and what I can do to fix this? I was reading what I can find on Google, and played around for 2 evenings, but didn't came up with a solution...

One more thing: Each chunk is a child of an empty "world" gameobject!

If you need more code, just let me know, I will post everything you need to know, but I didn't want to post too much code.

0002.png (25.9 kB)
0001.png (12.7 kB)
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

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.

Follow this Question

Answers Answers and Comments

58 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

Related Questions

How to get children from a Parent and not get the children of the children too 4 Answers

How to ungroup objects to its parent? 1 Answer

How do I set the pivot to the exact position as parent object? 0 Answers

Rotation problem in contructing arm with parent / child reloation (forearm move way faster) 0 Answers

Why my parent follows its child game object? 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