How do I want to instantiate object by series ?

HI all,
i want to instantiate objects one by one by series not Randomly, for randomly there is a code like
Random.Range(0,5);

but i want to instantiate the objects by series, like after instantiate object 1 i want to instantiate object 2 and after object 3.
Please help me how can i do this is there any code for that?

assuming you’re storing these “things” you want to instantiate in a list or array, just store the current “pointer” (i.e. the index value you are using) and increment it each time you use it.

so

array[++index];

etc.

you’ll need to handle wrap around or make sure the index doesn’t go out of bounds.

I have this code can you plz series in this??

Private int platformSelector;
public ObjectPooler_Controller[] theObjectPools;

void Update ()
    {
        if(transform.position.y < generationPoint.position.y)
        {

            platformSelector = Random.Range(0, theObjectPools.Length);
//This i dont want  in random

GameObject newPlatform = theObjectPools[platformSelector].GetPooledObject();
         }
     }

Something like this?

    private int platformSelector;
    public ObjectPooler_Controller[] theObjectPools;

    int index;
  
    void Update ()
    {
        if(transform.position.y < generationPoint.position.y)
        {
          
            platformSelector = theObjectPools[index];
            index = Mathf.Repeat(index++,theObjectPools.Length); //Handle wrapping of index
          
            GameObject newPlatform = theObjectPools[platformSelector].GetPooledObject();
        }
    }

thx i’ll try this …

Assets/Scripts/PlatformGenerator_Controller.cs(50,25): error CS0266: Cannot implicitly convert type float' to int’. An explicit conversion exists (are you missing a cast?)

Assets/Scripts/PlatformGenerator_Controller.cs(49,25): error CS0029: Cannot implicitly convert type ObjectPooler_Controller' to int’

no it has same error
Assets/Scripts/PlatformGenerator_Controller.cs(49,25): error CS0029: Cannot implicitly convert type ObjectPooler_Controller' to int’

Sorry, I didn’t read your code properly, this should do it.

public ObjectPooler_Controller[] theObjectPools;
  
    int index;
  
    void Update ()
    {
        if(transform.position.y < generationPoint.position.y)
        {
            index = Mathf.Repeat(index++,theObjectPools.Length); //Handle incrementing and wrapping of index
            GameObject newPlatform = theObjectPools[index].GetPooledObject();
        }
    }

Assets/Scripts/PlatformGenerator_Controller.cs(50,25): error CS0266: Cannot implicitly convert type float' to int’. An explicit conversion exists (are you missing a cast?

index = (int)Mathf.Repeat(index++,theObjectPools.Length); //Handle incrementing and wrapping of index

Sorry I forgot MathF.Repeat returns a float, typing these on the fly, however I would have hoped you might have been able to fix that one by yourself. :wink:

ya i also forget,
but here it generats only first arry object not the second or third…

Have you debug.logged index to confirm that it is incrementing and not being reset to zero anywhere? And also that your objectPools array actually has more than one element in it?

here is the whole code

using UnityEngine;
using System.Collections;

public class PlatformGenerator_Controller : MonoBehaviour
{
    //public GameObject thePlatform;
    public Transform generationPoint;
    public float distanceBetween;

    public float distanceBetweenMin;
    public float distanceBetweenMax;

    //private float platformWidth;

    //public GameObject[] thePlatforms;
    private int platformSelector;
    private float[] platformWidths;

    public ObjectPooler_Controller[] theObjectPools;
    int index;

    // P I C K U P //
    public float randomPicupThreshold;
    // P I C K U P //


    void Start ()
    {
        //platformWidth = 1.0f;
        platformWidths = new float[theObjectPools.Length];

        for (int i=0; i < theObjectPools.Length; i++)
        {
            platformWidths[i] = 1.0f;
        }


    }
   

    void Update ()
    {
        if(transform.position.y < generationPoint.position.y)
        {
            distanceBetween=Random.Range(distanceBetweenMin,distanceBetweenMax);

            //platformSelector = Random.Range(0, theObjectPools.Length);

            //platformSelector = theObjectPools[index];
            index = (int)Mathf.Repeat(index++, theObjectPools.Length);

            transform.position = new Vector3(transform.position.x,transform.position.y  + platformWidths[platformSelector]+ distanceBetween,transform.position.z);


            //Instantiate (/* thePlatform */ thePlatforms[platformSelector], transform.position, Quaternion.identity);


            GameObject newPlatform = theObjectPools[index].GetPooledObject();

            newPlatform.transform.position = transform.position;
            newPlatform.transform.rotation= transform.rotation;
            newPlatform.SetActive(true);

            //transform.position = new Vector3(transform.position.x,transform.position.y + (platformWidths[platformSelector] / 2) ,transform.position.z);

            // P I C K U P //
            if(Random.Range(0, 100) < randomPicupThreshold)
            {

            PickupGenerator_Controller.instance.SpawnPickups(new Vector3(transform.position.x+(Random.Range(-3,3)),transform.position.y+1f,transform.position.z));

            }
            // P I C K U P //
        }
    //
        if (Input.GetKey("escape"))
            Application.Quit();
    }
}

ok i fixed this
it works fine thanks

change index++ to index + 1

[code=CSharp]index = (int)Mathf.Repeat(index + 1,theObjectPools.Length); //Handle incrementing and wrapping of index

Edit: Looks like you beat me to it :slight_smile:

GameObject newPlatform = theObjectPools[index++].GetPooledObject();

in increament the index

That’s fine, how are you handling the index going out of range?

Nevermind, I see, if you still have the mathf.repeat line in there, that will do it, but ideally you should change the line as I suggested last, and use the variable index to retrieve the pooled object.

its not going its looping
wait i have to check…

its working good , if any thing i’ll post it
Thanks

Glad to hear you got it working :slight_smile: