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
0
Question by yarik4play · May 23, 2018 at 10:29 AM · instantiateprefabsbackgrounddestroy objectlooping

Wrong coordinates of setting position of the object

I need to make loop background moving(vertically). I have two bg in me scene, when one collide a collider(created for detecting that bg is not on screen space), then this bg is destroying and in this moment i Instantiate a new bg. When i Instantiate a new bg in right coordinates, i have small gap between my two bgs in scene(one bg is old and one recently spawned) I tried to use prefab or just gameobject anyway i have a gap. There is my collider's script:

 `
  void OnTriggerEnter2D(Collider2D collider)
         {
             BgControllerGp.instance.SpawnBg();
             Destroy(collider.gameObject);
     
         }
 `

And my SpawnBg method

 `
 void SpawnBg(){
 Instantiate(backGp, new Vector2(0f, 120f), Quaternion.identity);
 }

`

120f is my background collider.size.y and of course it is right coordinates

I tried to scale my y size from 1 to 1.005 and it is works , but expected after 20 spawns or more(i mean some time) i have gap between bgs. I knew this do not good and some time i'll see this gap causes by scaling Help me please, i need Instantiating for my bgs, cause i wanna change sprite of bgs and with Instantiate it is easy.Thank You

Comment
Add comment · Show 1
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 misher · May 24, 2018 at 09:36 AM 0
Share

Do not scale. $$anonymous$$ake sure vertical size of your bg gameobjects is some round value (like 100 is better than 107.589 ). $$anonymous$$ake your background gameobject's pivot at the bottom. Before Destroying old BG, use its exact position plus vertical offset (old bg height) to calculate desired position of new BG (and I assume you want to see both, the new bg and old bg at the ame time and destroy old bg only when it goes out of the screen)... Al that being said, you better find a best practice way to achieve what you want, because it seems like not an optimal way to make moving background.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Timbaluke · May 24, 2018 at 10:45 AM

I had exactly the same problem when I was working with moving procedural floor-tiles. Parent all backgrounds to an empty gameobject and set the localPosition instead of the world position.

Comment
Add comment · 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

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

104 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

Related Questions

Make a game object in a scene into a prefab with script 1 Answer

Need help understanding scripted prefab behavior - when I click one, the script runs on ALL prefab instances, not just the one I clicked. 3 Answers

Animations on instantiated prefabs not working 0 Answers

Instantiate an object in a prefab? 1 Answer

Instantiate only certain objects within a 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