- Home /
How do I search a list for a Vector3?
I have a list that holds a number of Vector3s, and before I add another Vector3 to the list, I want to check to make sure the list doesn't already contain that value. When I use list.Exists(), it gives me an error:
"The best overloaded method match for `System.Collections.Generic.List.Exists(System.Predicate)' has some invalid arguments"
I've tried inputting the value I need to search for in different ways, but I always get this error. Does anyone know what I'm doing wrong? The error occurs at line 88 in the CheckForValidSpots function. Any advice would be super helpful!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LevelCreatorScript : MonoBehaviour {
public GameObject roomPrefab;
public int roomCount = 5;
int doorCount = 0;
public float roomHeight = 15.0f;
public float roomWidth = 24.0f;
List<Vector3> pastRooms = new List<Vector3>();
Vector3 currentRoom;
List<Vector3> futureRooms = new List<Vector3>();
List<Vector3> validSpots = new List<Vector3>();
List<Vector3> doorDirections = new List<Vector3>();
void Start () {
futureRooms.Add(new Vector3(0, 0, 0));
CreateLevel();
}
void CreateLevel() {
while ( (futureRooms.Count + pastRooms.Count + 1) < roomCount ) {
if (doorCount == 0) {
validSpots.Clear();
if (true) {
pastRooms.Add(currentRoom);
Debug.Log( "Adding current room: " + currentRoom + "to pastRooms at Index[" + (pastRooms.Count - 1) + "].");
}if (futureRooms.Count > 0) {
currentRoom = futureRooms[0];
futureRooms.RemoveAt (0);
if(pastRooms.Count == 0) {
doorCount = Random.Range(2,5);
}else {
doorCount = Random.Range(1,5);
}
}
}else {
//tries to make a new room
//determine valid placement spots for new rooms
//puts all valid placement spots in a new list: validSpots<vector3>
//if validSpots.count < doorCount, DoorCount = validSpots.Count
//randomly chooses between one of the rooms to make a new room
}
}
}
void AddDirections(List<Vector3> directions) {
directions.Add(new Vector3(0, 0, roomHeight));
directions.Add(new Vector3(roomWidth, 0, 0));
directions.Add(new Vector3(0, 0, -roomHeight));
directions.Add(new Vector3(-roomWidth, 0, 0));
}
void CheckForValidSpots(Vector3 roomLocation) {
for (int n = 0; n < 4; n++) {
Vector3 locationToCheck = new Vector3( roomLocation.x + doorDirections[n].x,
0, roomLocation.z + doorDirections[n].z);
//HERE IS WHERE I GET THE ERROR
if( !pastRooms.Exists(locationToCheck) && !futureRooms.Exists(locationToCheck) ) {
validSpots.Add(locationToCheck);
}
}
}
}
Answer by robertbu · Aug 11, 2013 at 08:09 PM
For a direct comparison, use Contains():
if( !pastRooms.Contains(locationToCheck) && !futureRooms.Contains(locationToCheck) ) {
Your answer
Follow this Question
Related Questions
IComparable error due to float 1 Answer
NullReferenceExeption 1 Answer
Unknown Identifier 'List' 1 Answer
why this does not work (Lists) 3 Answers
When the editor is launched in unity3d, it displays a white screen and an error 1 Answer