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.