Question by
MohammadAlizadeh · Aug 08, 2017 at 06:38 AM ·
arraylinerendererduplicate
Remove Duplicated Objects in Array
Hi, I'm trying to compare all my LineRenderers with each other and see if there're any dublicated LineRenderers with same values but this code destroys all:
public void RemoveDublicated () {
//get all drawn lines
LineRenderer[] lines = GameObject.FindObjectsOfType <LineRenderer>();
for (var i = 0; i < lines.Length; i++) {
var store = lines [i].GetComponent <StoreLineRenderValues> ();
//remove dublicated paintings
if (i > 0) {
for (var j = 0; j < lines.Length; j++) {
var lastStore = lines [j].GetComponent <StoreLineRenderValues> ();
if (store.theColor == lastStore.theColor && store.savedPos.Length == lastStore.savedPos.Length &&
store.width == lastStore.width) {
Destroy (store.gameObject);
print (store.name + " Destroyed");
}
}
}
}
}
Comment
Best Answer
Answer by MohammadAlizadeh · Aug 09, 2017 at 06:25 AM
Some one Helped me to fix it by checking each gameobject with previous one or next one:
By adding: for (var j = i + 1; j < lines.Length; j++) {
public void RemoveDublicated () {
//get all drawning lines
LineRenderer[] lines = GameObject.FindObjectsOfType <LineRenderer>();
//loop
for (var i = 0; i < lines.Length; i++) {
var store = lines [i].GetComponent <StoreLineRenderValues> ();
//remove dublicated paintings
if (store != null) {
for (var j = i + 1; j < lines.Length; j++) {
var lastStore = lines [j].GetComponent <StoreLineRenderValues> ();
if (lastStore != null) {
if (store.theColor == lastStore.theColor && store.savedPos.Length == lastStore.savedPos.Length &&
store.width == lastStore.width) {
Destroy (store.gameObject);
print (store.name + " Destroyed");
}
}
}
}
}
}
Your answer
