0
\$\begingroup\$

I have in this case 10 objects. Each object have this script attached:

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class ShowMeshBounds : MonoBehaviour
{
    public GameObject prefabEffect;
    public Color color = Color.green;

    private Vector3 v3FrontTopLeft;
    private Vector3 v3FrontTopRight;
    private Vector3 v3FrontBottomLeft;
    private Vector3 v3FrontBottomRight;
    private Vector3 v3BackTopLeft;
    private Vector3 v3BackTopRight;
    private Vector3 v3BackBottomLeft;
    private Vector3 v3BackBottomRight;

    private float counter = 0;
    public bool animateLines;
    public float speed = 1f;

    private List<GameObject> allLines = new List<GameObject>();
    private List<GameObject> instancesToMove = new List<GameObject>();
    private Vector3 endPos;

    private void Start()
    {
        CalcPositons();
        DrawBox();
        allLines = GameObject.FindGameObjectsWithTag("FrameLine").ToList();

        DuplicatePrefabEffects(allLines.Count);
        instancesToMove = GameObject.FindGameObjectsWithTag("Duplicated Prefab").ToList();

        StartCoroutine(moveStuff());
    }

    private void DuplicatePrefabEffects(int duplicationNumber)
    {
        for (int i = 0; i < duplicationNumber; i++)
        {
            var go = Instantiate(prefabEffect);
            go.tag = "Duplicated Prefab";
            go.name = "Duplicated Prefab";
        }
    }

    void CalcPositons()
    {
        Bounds bounds = GetComponent<MeshFilter>().sharedMesh.bounds;

        Vector3 v3Center = bounds.center;
        Vector3 v3Extents = bounds.extents;

        v3FrontTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top left corner
        v3FrontTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top right corner
        v3FrontBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom left corner
        v3FrontBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom right corner
        v3BackTopLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top left corner
        v3BackTopRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top right corner
        v3BackBottomLeft = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom left corner
        v3BackBottomRight = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom right corner

        v3FrontTopLeft = transform.TransformPoint(v3FrontTopLeft);
        v3FrontTopRight = transform.TransformPoint(v3FrontTopRight);
        v3FrontBottomLeft = transform.TransformPoint(v3FrontBottomLeft);
        v3FrontBottomRight = transform.TransformPoint(v3FrontBottomRight);
        v3BackTopLeft = transform.TransformPoint(v3BackTopLeft);
        v3BackTopRight = transform.TransformPoint(v3BackTopRight);
        v3BackBottomLeft = transform.TransformPoint(v3BackBottomLeft);
        v3BackBottomRight = transform.TransformPoint(v3BackBottomRight);
    }

    void DrawBox()
    {
        SpawnLineGenerator(v3FrontTopLeft, v3FrontTopRight, color);
        SpawnLineGenerator(v3FrontTopRight, v3FrontBottomRight, color);
        SpawnLineGenerator(v3FrontBottomRight, v3FrontBottomLeft, color);
        SpawnLineGenerator(v3FrontBottomLeft, v3FrontTopLeft, color);

        SpawnLineGenerator(v3BackTopLeft, v3BackTopRight, color);
        SpawnLineGenerator(v3BackTopRight, v3BackBottomRight, color);
        SpawnLineGenerator(v3BackBottomRight, v3BackBottomLeft, color);
        SpawnLineGenerator(v3BackBottomLeft, v3BackTopLeft, color);

        SpawnLineGenerator(v3FrontTopLeft, v3BackTopLeft, color);
        SpawnLineGenerator(v3FrontTopRight, v3BackTopRight, color);
        SpawnLineGenerator(v3FrontBottomRight, v3BackBottomRight, color);
        SpawnLineGenerator(v3FrontBottomLeft, v3BackBottomLeft, color);
    }

    void SpawnLineGenerator(Vector3 start, Vector3 end, Color color)
    {
        GameObject myLine = new GameObject();

        myLine.tag = "FrameLine";
        myLine.name = "FrameLine";

        myLine.AddComponent<LineRenderer>();
        myLine.AddComponent<EndHolder>();
        myLine.GetComponent<EndHolder>().EndVector = end;
        LineRenderer lr = myLine.GetComponent<LineRenderer>();
        lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
        lr.startColor = color;
        lr.useWorldSpace = false;
        lr.endColor = color;
        lr.startWidth = 0.1f;//0.03f;
        lr.endWidth = 0.1f;//0.03f;
        lr.SetPosition(0, start);
        lr.SetPosition(1, start);
    }

    IEnumerator moveStuff()
    {
        for (int i = 0; i < allLines.Count; i++)
        {
            counter = 0;

            while (Vector3.Distance(instancesToMove[i].transform.position, endPos) > 0.1f)
            {
                counter++;
                endPos = allLines[i].GetComponent<EndHolder>().EndVector;
                Vector3 startPos = allLines[i].GetComponent<LineRenderer>().GetPosition(0);
                Vector3 tempPos = Vector3.Lerp(startPos, endPos, counter / 500f * speed);

                instancesToMove[i].transform.position =
                    Vector3.MoveTowards(startPos, endPos, counter / 25f * speed);

                allLines[i].GetComponent<LineRenderer>().SetPosition(1, instancesToMove[i].transform.position);//tempPos);

                //move towards destination
                yield return null;
            }
        }
    }
}

The problem is that each gameobject find objects name "Frame Line" And then in the Start this line:

allLines = GameObject.FindGameObjectsWithTag("FrameLine").ToList();

Will find each time more and more gamoebjects and will duplicate on this line:

DuplicatePrefabEffects(allLines.Count);

Too many objects. First it find 12 objects then 24 36 48 and so on. But it should or what I want it to find each 12 gameobjects in the end it should have 120 frame line.

\$\endgroup\$
2
\$\begingroup\$

This is why it behaves that way:

  • Start() is called on the first of your 10 game object.
  • DrawBox() is called, which in turns calls 12 time SpawnLineGenerator()
  • Every time SpawnLineGenerator() is called, you create a new game object with the line GameObject myLine = new GameObject();: these new game objects are all instantiated in the root of your scene
  • allLines = GameObject.FindGameObjectsWithTag("FrameLine").ToList(); is called: for the first time it will find the first 12 game object

But, when the second Start() is called, the code will add another 12 new game objects in the root, so now you have 24 game objects with the tag FrameLine, and they will be added to the allLines list. The 10th time Start() is called, the code will add all the 120 game objects to FrameLine.

To avoid this, and to assign exactly 12 game objects to each of these 10 objects, you only need to children these 12 game objects to each of the 10 game objects with the script, with this code:

private void Start()
{
    CalcPositons();
    DrawBox();
    //With this foreach you check all children, and add only the ones with the appropriate tag
    foreach (Transform item in transform) {
        if (item.gameObject.tag == "FrameLine") {
            allLines.Add(item.gameObject);
        }
    }

    //Do rest of your stuff
}

void SpawnLineGenerator(Vector3 start, Vector3 end, Color color)
{
    GameObject myLine = new GameObject();
    //Make myLine a child of this game object
    myLine.transform.parent = transform;

    //Do all your other stuff
}
\$\endgroup\$
  • \$\begingroup\$ @user1196715 if the answer solves your problem, please tag it as accepted. If not, leave a comment and elaborate what else you need. \$\endgroup\$ – Galandil Feb 19 '18 at 9:27

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.