using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteAlways]
public class GenerateCurvedLines : MonoBehaviour
{
public GameObject linesPointsPrefab;
public GameObject linesParent;
[Range(3, 100)]
public int numberOfLines;
[Range(5, 50)]
public int gap;
public bool randomLinesPositions;
public bool generateLines;
public bool clearPositions;
private List<Vector3> randomPositions = new List<Vector3>();
private GameObject instance;
// Start is called before the first frame update
void Start()
{
if (randomLinesPositions)
{
for (int i = 0; i < numberOfLines; i++)
{
randomPositions.Add(new Vector3(UnityEngine.Random.Range(5, 50), UnityEngine.Random.Range(5, 50), UnityEngine.Random.Range(5, 50)));
}
}
GenerateLines();
}
// Update is called once per frame
void Update()
{
if (clearPositions)
{
var lr = GetComponent<LineRenderer>();
lr.positionCount = 0;
clearPositions = false;
}
if (generateLines)
{
GenerateLines();
generateLines = false;
}
}
private void GenerateLines()
{
for (int i = 0; i < numberOfLines; i++)
{
randomPositions.Add(new Vector3(UnityEngine.Random.Range(5, 50), UnityEngine.Random.Range(5, 50), UnityEngine.Random.Range(5, 50)));
if (randomLinesPositions)
{
instance = Instantiate(
linesPointsPrefab,
randomPositions[i],
Quaternion.identity
);
}
else
{
instance = Instantiate(
linesPointsPrefab,
Vector3.right * gap * i,
Quaternion.identity
);
}
instance.transform.parent = linesParent.transform;
}
}
}
At this part I'm trying to Instantiate random positions of the prefabs but each time I check true the flag generateLines it's Instantiating the prefabs at the same positions instead each time to Instantiate them in a new random positions. It's like it's Instantiate once random positions and then using this positions each time and not Instantiating new random positions.
randomPositions.Add(new Vector3(UnityEngine.Random.Range(5, 50), UnityEngine.Random.Range(5, 50), UnityEngine.Random.Range(5, 50)));
if (randomLinesPositions)
{
instance = Instantiate(
linesPointsPrefab,
randomPositions[i],
Quaternion.identity
);
}