Hi all, I’ve done some simple object generation script, how can I randomize objects per instatiation? not just instatiate all objects from first class list then from second and so on?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Test : MonoBehaviour {
public List <Obstacles> obs = new List<Obstacles>();
public Vector3 StartPos = Vector3.zero;
private Vector3 nextPos;
void Start ()
{
LoadObstacles();
}
void LoadObstacles()
{
nextPos = Vector3.zero;
foreach (Obstacles o in obs)
{
for(int x = 0; x < o.qty; x++)
{
nextPos.y = StartPos.y + Random.Range(o.Positions.min.y, o.Positions.max.y);
Instantiate (o.Prefab, StartPos + nextPos, Quaternion.identity);
nextPos.x += Random.Range(o.Positions.min.x, o.Positions.max.x);
}
}
}
}
[System.Serializable]
public class Obstacles
{
public Transform Prefab;
public int qty;
public pos Positions;
}
[System.Serializable]
public class pos
{
public Vector2 max;
public Vector2 min;
}
Maybe this is what you were looking for:
for(int x = 0; x < 10; x++) // or how many you want to be instantiated
{
nextPos.y = StartPos.y + Random.Range(o.Positions.min.y, o.Positions.max.y);
int randIndex = Random.Range(0, obs.Count);
Instantiate(obs[randIndex].Prefab, StartPos + nextPos, Quaternion.identity);
nextPos.x += Random.Range(o.Positions.min.x, o.Positions.max.x);
}
}
Yeh, this way works, but it also shuffle position values, so wont work as I need…
Looks like adding the same index to it do the job, let me make some tests.
Yeh, looks like I’ve asked wrong question, random index was the first thing I have done, but more complex thing for me is to reset/reload Instatiated objects(Randomize them again). I’ve create a List of Transforms in Obstacles Class called “preloadedObstacles”
void LoadObstacles()
{
nextPos = Vector3.zero;
foreach (Obstacles o in obs)
{
for(int x = 0; x < o.qty; x++)
{
int randIndex = Random.Range(0, obs.Count);
nextPos.y = StartPos.y + Random.Range(obs[randIndex].Positions.min.y, obs[randIndex].Positions.max.y);
obs[randIndex].preloadedObstacles.Add ((Transform) Instantiate (obs[randIndex].Prefab, StartPos + nextPos, Quaternion.identity));
nextPos.x += Random.Range(obs[randIndex].Positions.min.x, obs[randIndex].Positions.max.x);
}
}
}
void Reset()
{
nextPos = Vector3.zero;
foreach (Obstacles o in obs)
{
for(int x = 0; x < o.preloadedObstacles.Count; x++)
{
nextPos.y = StartPos.y + Random.Range(o.Positions.min.y, o.Positions.max.y);
o.preloadedObstacles[x].position = StartPos + nextPos;
nextPos.x += Random.Range(o.Positions.min.x, o.Positions.max.x);
}
}
}
How to? the same way with random index I got null refference…
Also there is a bug with objects amount, if for example there is 5 “qty” it means there should be 5 objects of each… but amount also became random. Apparently, this approach completely wrong, any ideas ???
What do you mean by reset/reload instantiated objects again if you want them to be randomized again? I think i do not quite understand what you want to achieve. Can you describe an example?