Search Unity

How do I want to instantiate object by series ?

Discussion in 'Scripting' started by babji3, Nov 29, 2015.

  1. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    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?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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.
     
  3. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    I have this code can you plz series in this??
    Code (CSharp):
    1. Private int platformSelector;
    2. public ObjectPooler_Controller[] theObjectPools;
    3.  
    4. void Update ()
    5.     {
    6.         if(transform.position.y < generationPoint.position.y)
    7.         {
    8.  
    9.             platformSelector = Random.Range(0, theObjectPools.Length);
    10. //This i dont want  in random
    11.  
    12. GameObject newPlatform = theObjectPools[platformSelector].GetPooledObject();
    13.          }
    14.      }
     
    Last edited: Dec 1, 2015
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Something like this?

    Code (CSharp):
    1.  
    2.     private int platformSelector;
    3.     public ObjectPooler_Controller[] theObjectPools;
    4.  
    5.     int index;
    6.  
    7.     void Update ()
    8.     {
    9.         if(transform.position.y < generationPoint.position.y)
    10.         {
    11.          
    12.             platformSelector = theObjectPools[index];
    13.             index = Mathf.Repeat(index++,theObjectPools.Length); //Handle wrapping of index
    14.          
    15.             GameObject newPlatform = theObjectPools[platformSelector].GetPooledObject();
    16.         }
    17.     }
     
  5. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    thx i'll try this ..
     
  6. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    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'
     
  7. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    no it has same error
    Assets/Scripts/PlatformGenerator_Controller.cs(49,25): error CS0029: Cannot implicitly convert type `ObjectPooler_Controller' to `int'
     
  8. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Sorry, I didn't read your code properly, this should do it.

    Code (CSharp):
    1. public ObjectPooler_Controller[] theObjectPools;
    2.  
    3.     int index;
    4.  
    5.     void Update ()
    6.     {
    7.         if(transform.position.y < generationPoint.position.y)
    8.         {
    9.             index = Mathf.Repeat(index++,theObjectPools.Length); //Handle incrementing and wrapping of index
    10.             GameObject newPlatform = theObjectPools[index].GetPooledObject();
    11.         }
    12.     }
    13.  
     
  9. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    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?
     
  10. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Code (CSharp):
    1. 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. ;)
     
  11. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    ya i also forget,
    but here it generats only first arry object not the second or third..
     
  12. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    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?
     
  13. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    here is the whole code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlatformGenerator_Controller : MonoBehaviour
    5. {
    6.     //public GameObject thePlatform;
    7.     public Transform generationPoint;
    8.     public float distanceBetween;
    9.  
    10.     public float distanceBetweenMin;
    11.     public float distanceBetweenMax;
    12.  
    13.     //private float platformWidth;
    14.  
    15.     //public GameObject[] thePlatforms;
    16.     private int platformSelector;
    17.     private float[] platformWidths;
    18.  
    19.     public ObjectPooler_Controller[] theObjectPools;
    20.     int index;
    21.  
    22.     // P I C K U P //
    23.     public float randomPicupThreshold;
    24.     // P I C K U P //
    25.  
    26.  
    27.     void Start ()
    28.     {
    29.         //platformWidth = 1.0f;
    30.         platformWidths = new float[theObjectPools.Length];
    31.  
    32.         for (int i=0; i < theObjectPools.Length; i++)
    33.         {
    34.             platformWidths[i] = 1.0f;
    35.         }
    36.  
    37.  
    38.     }
    39.    
    40.  
    41.     void Update ()
    42.     {
    43.         if(transform.position.y < generationPoint.position.y)
    44.         {
    45.             distanceBetween=Random.Range(distanceBetweenMin,distanceBetweenMax);
    46.  
    47.             //platformSelector = Random.Range(0, theObjectPools.Length);
    48.  
    49.             //platformSelector = theObjectPools[index];
    50.             index = (int)Mathf.Repeat(index++, theObjectPools.Length);
    51.  
    52.             transform.position = new Vector3(transform.position.x,transform.position.y  + platformWidths[platformSelector]+ distanceBetween,transform.position.z);
    53.  
    54.  
    55.             //Instantiate (/* thePlatform */ thePlatforms[platformSelector], transform.position, Quaternion.identity);
    56.  
    57.  
    58.             GameObject newPlatform = theObjectPools[index].GetPooledObject();
    59.  
    60.             newPlatform.transform.position = transform.position;
    61.             newPlatform.transform.rotation= transform.rotation;
    62.             newPlatform.SetActive(true);
    63.  
    64.             //transform.position = new Vector3(transform.position.x,transform.position.y + (platformWidths[platformSelector] / 2) ,transform.position.z);
    65.  
    66.             // P I C K U P //
    67.             if(Random.Range(0, 100) < randomPicupThreshold)
    68.             {
    69.  
    70.             PickupGenerator_Controller.instance.SpawnPickups(new Vector3(transform.position.x+(Random.Range(-3,3)),transform.position.y+1f,transform.position.z));
    71.  
    72.             }
    73.             // P I C K U P //
    74.         }
    75.     //
    76.         if (Input.GetKey("escape"))
    77.             Application.Quit();
    78.     }
    79. }
    80.  
     
  14. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    ok i fixed this
    it works fine thanks
     
  15. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    change index++ to index + 1

    Code (CSharp):
    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 :)
     
  16. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    GameObject newPlatform = theObjectPools[index++].GetPooledObject();

    in increament the index
     
  17. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    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.
     
  18. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    its not going its looping
    wait i have to check..
     
  19. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    its working good , if any thing i'll post it
    Thanks
     
  20. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Glad to hear you got it working :)