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.
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();
}
}
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?
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?
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();
}
}
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.