- Home /
 
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


     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
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.
              }
     
 
 
 
              it worked but only for the first platform the others are still stacked on top of the one in the bottom 
Sorry, missed a line of code that re-assigns 'generate' to the transfomr of the object you've just created. Does it work now?
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!
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!
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.
Your answer