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 aa1000 · Apr 24, 2015 at 07:42 PM · c#unity 5instantiatescript.endless runner

How do I make the platform spawn after the other one

I am making and Endless runner where I want to spawn a platform everytime the player sets a trigger but when I do the platforms spawn all in the same place on top of each other how do i change that, sorry but I can explain it in English very well

alt text

alt text

     public bool needToGenerate = false;
     public Transform generate;
     // Update is called once per frame
     void Update () {
         if (needToGenerate)
             GeneratePlatform ();
     }
 
     void GeneratePlatform()
     {
         GameObject obj = ObjectPooler.current.GetPooledObject ();
 
         if (obj == null)
             return;
         obj.transform.position = generate.transform.position;
         obj.transform.rotation = transform.rotation;
         obj.SetActive (true);
         needToGenerate = false;
     }

the Transform generate here is a gameboject at the edge of the platform

8.png (94.6 kB)
9.png (73.6 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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by DavidZendle · Apr 24, 2015 at 10:57 PM

I believe the problem with your code is that you're generating new platforms at the same location as your old platforms - but not moving them over on the x-axis by some amount corresponding to the old platform's width. Essentially you're dropping all your platforms on top of each other!

What you need to do is shove your new platform over a little bit so that it's not on top of the earlier platform anymore. I'm assuming that you have a BoxCollider2D attached to your platforms (I think I spied something like that in your screenshots).

If this is the case, then try:

 public bool needToGenerate = false; // This holds a value that tells you whether you need to make a new platform
      public Transform generate; //This holds the position of the last object that you generated. At the beginning, I'd guess that you dragged and dropped your first platform into here in the editor?
      // Update is called once per frame
      void Update () {
          if (needToGenerate) // If you need to generate a platform
              GeneratePlatform (); // Then run a function that generates a new platform
      }
  
       void GeneratePlatform()
              {
                  GameObject obj = ObjectPooler.current.GetPooledObject (); // Get the object in the pool that you want to place as the next platform in the level, and assign it to the variable 'obj'
          
                  if (obj == null){ // if there's no such object
                      return; // don't do anything
     }
     
     
                    float width = generate.GetComponent<BoxCollider2D>().size.x; //otherwise, get the width of the previous platform's collider
         
         
                  obj.transform.position = generate.position+new Vector3(width,0f,0f); //place your new platform at the previous one's position plus its width (i.e. put it next to the previous platform)
                  obj.transform.rotation = generate.transform.rotation; //rotate it by the same amount as the previous platform (something similar was in your original script so I'm leaving it here in case it does something I'm unaware of)
                  obj.SetActive (true); //Set your new platform to active
                  needToGenerate = false; //Tell the script that you don't need to generate a new platform.
                  generate = obj.transform; //Tell the script that the next platform it makes should be placed next to the one it just built, rather than an earlier one.
              }
     
 
 
Comment
Add comment · Show 14 · 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 aa1000 · Apr 25, 2015 at 11:35 AM 0
Share

it worked but only for the first platform the others are still stacked on top of the one in the bottom alt text

11.png (65.5 kB)
avatar image DavidZendle · Apr 25, 2015 at 01:32 PM 1
Share

Sorry, missed a line of code that re-assigns 'generate' to the transfomr of the object you've just created. Does it work now?

avatar image DavidZendle · Apr 25, 2015 at 01:58 PM 1
Share

That's okay. Could you clarify: Do what? I've edited the code above, if you copy and paste that to where your old code was then it should work. I'll add some comments explaining why it does what it does too. P.s. if this works, please mark it as the correct answer!

avatar image DavidZendle · Apr 25, 2015 at 02:31 PM 1
Share

The problem you're having is not in this code, but ins$$anonymous$$d is to do with your ObjectPooler class: Basically, the theory behind pooling objects is that rather than create and destroy them on the fly, you have a 'pool' of inactive objects: When you need an object, you grab it out of the pool and use it; when you don't need it anymore, you return it to the pool.

What I believe must be happening here is that whilst you're grabbing 'obj' from your pool you must be not registering that it has been removed from the pool, so it gets reassigned inappropriately every time you need to create a new platform. In any case, I believe the problem you're having is in your ObjectPooler code, not this code. If you posted it then it would be possible to debug!

avatar image DavidZendle · Apr 25, 2015 at 03:30 PM 1
Share

Actually, I can see the problem.

When you changed the code, you changed generate = obj.transform to generate.transform.position = obj.transform.position. These two lines do not mean the same thing. One assigns obj's transform to the variable generate - the other moves generate to the position of obj. Change them back to what they were before and the code will work.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Instantiate prefab (button, texture) with attached script 1 Answer

Object Spawner 1 Answer

Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't? 1 Answer

How to Spawn after checking if the clones are destroyed. 1 Answer

Distribute terrain in zones 3 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