Question by
JackAshwell · Jun 24, 2020 at 04:51 PM ·
2darraylist
Compare each vector3 from an array to each vector3 in a list and add objects which do not exist into the list
So I have this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapGenerator : MonoBehaviour {
public List<GameObject> middlePoints = new List<GameObject>();
public bool notSame = true;
void Update() {
if(Input.GetButtonDown("Fire2")) {
f();
}
}
void f() {
MiddleChecker();
}
void MiddleChecker() {
GameObject[] middlePointsTemp = GameObject.FindGameObjectsWithTag("MiddlePoint");
foreach(var i in middlePointsTemp) {
foreach(var j in middlePoints) {
if(i.transform.position == j.transform.position) {
// Object already exists in loop
notSame = false;
}
}
if(notSame == true) {
// Object doesn't exist in middlePoints
int index = System.Array.IndexOf(middlePointsTemp, i);
middlePoints.Add(middlePointsTemp[index]);
}
}
foreach(GameObject i in middlePoints) {
i.SetActive(false);
i.SetActive(true);
}
}
}
And I want to compare each vector3 value in middlePointsTemp to each value in middlePoints and if there are any objects in middlePointsTemp that do not have the same vector3 as the objects in middlePoints, I want to add them into the list. But there seems to be a problem with my code
I know that sounds kinda confusing, sorry.
Thanks!
Comment
Your answer
Follow this Question
Related Questions
Sort a list of gameObjects and remove any duplicates one using vector3 positions 0 Answers
How to declare a list of arrays? 1 Answer
[C#] Creating and Importing an Array from a separate file 2 Answers
Having trouble with making game object change sprites with an array? 0 Answers
Assign values to array elements 1 Answer