0
\$\begingroup\$
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
                       );
                }
\$\endgroup\$
2
\$\begingroup\$

It's like it's Instantiate once random positions and then using this positions each time and not Instantiating new random positions.

That's exactly what your code tells it to do. randomPositions is a member variable, so any values you add to it are going to stay there unless you overwrite or remove them. You first do this from Start():

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)));
}

You've already filled the list with some values. These values are never cleared or overwritten in your code.

Later you do this:

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)));

You're calling Add() here, so it's adding new random values to the end of the list. Your list of positions just keeps getting longer and longer, but the elements in each index never change.

The list is pointless and is just wasting memory. Write your code like this:

if (randomLinesPositions) {
    var position = new Vector3(UnityEngine.Random.Range(5, 50), UnityEngine.Random.Range(5, 50), UnityEngine.Random.Range(5, 50));
    instance = Instantiate(
        linesPointsPrefab,
        position,
        Quaternion.identity
    );
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.