Hi,I have a question about lists,which I can create and add to without any issues.
The problem Im having is comparing a variable(vector3) with the list(vector3) to test if it already exists.
Vector3 currenttilecord;
public Listtowers = new List();
inside update
Debug.Log(“current tile coord :” + currenttilecord);
towers.Add(currenttilecord);
//everything works above,its the next line of code that brings up a error
if(currenttilecord != towers){//cannot convert error
operator != cannot be applied to unityengine vector3 and systems collections generic list unity engine vector 3
How do I get round this error to test if the (currenttilecoord) does not have a (towers)placed there?
Thanks for looking.
Code tags, please!
Are you looking for List.Contains?
First, yes, please use code tags. It’s easier to help if things are formatted correctly and, thus, easier to read.
Second…
Here you’re comparing a thing of type Vector3 to a thing of type List. The list contains Vector3s, but it is still a List. To the computer it makes no sense to compare of a Vector3 and a List are equivalent.
As BoredMormon says, what you probably want to be doing is checking if the list contains an equivalent Vector3. You might be able to do that with the Contains method, and if that works… job done. However, you might need to do something more complex - Vector3s are made of floating point numbers, and checking those for equality is notoriously unreliable. Instead what you probably want to do is loop through the contents of the List and check each Vector3 for being near your comparison position. If it’s very close then consider it to be the same and act accordingly.
Thanks for the help guys,the contains does work after a little practice,heres the line of code that works
if(towers.Contains(currenttilecord)){
Debug.Log("cannot place on top of object");
This means after every object I place on my grid gets added to the list(towers) and now I can not place objects on top of eachother with 1 line of code for all objects instead of being very lengthy.
Again thxs v.much