Can you compare two Transform.positions? (ie Vector 3's?)

Hello, I have having some trouble with a script and I am wondering if it is because I try to compare two Vector 3’s and see if they are the same. I am wondering if that maybe doesnt work and somehow I need to use Vector3.distance instead?

Basically I loop through an array of GameObjects that have been instantiated, and I add them into a dictionary. But before I add them, I check to see if there is already a gameObject in the dictionary with the same position, that way it doesnt duplicate and add it again. Problem is, whenever I add more GameObjects, they are all saying that they are at the same position as the last one…

code:

public class SquareFootage : MonoBehaviour 
{
	//array to hold all the line cubes
	GameObject[] lines;
	private float lastLineCount = 0;
	
	public Dictionary > xAxisMap = new Dictionary>();
	
	// Update is called once per frame
	void Update () 
	{
		lines = GameObject.FindGameObjectsWithTag("Line");
		
		if(lines.Length > lastLineCount)
		{
			print ("new line added");
			BuildLineGroups();
			lastLineCount = lines.Length;
		}
		

	}
	
	void BuildLineGroups()
	{
		
		//loop through array of lines
		for(int cnt = 0; cnt < lines.Length; cnt++)
		{
			print ("Looping through lines");
			var x = lines[cnt].transform.position.x;
			
			//Check to see if there is already an entry for x
			//if there is, then it returns true otherwise its false
			List myList;
			bool wasFound = xAxisMap.TryGetValue(x, out myList);
			
			if(wasFound)
			{
				

			print("matching group found for value " + x);
			//check if this is a line that was already put into a group 
			//we do this by checking the "key" for the entry that it matches and we check to see if there is a matching transform.position in that entry
			

for(int i = 0; i<myList.Count; i++)
				{
					//if the position of this line matches that of any in the listed with key "X" then get outta here..
					if(myList*.transform.position == lines[cnt].transform.position)*
  •  			{*
    
  •  				print ("existing line found that matches this position, no line will be added");*
    
  •  				return;*
    
  •  			}*
    
  •  			else*
    
  •  			{*
    
  •  				print ("line added to existing group" + x);*
    
  •  				//otherwise, add this specific cube "line" to that group.*
    
  •  				xAxisMap[x].Add(lines[cnt]);*
    
  •  			}	*
    
  •  		}*
    
  •  	}*
    
  •  	//if no group with this position was found..*
    
  •  	if(!wasFound)*
    
  •  	{*
    
  •  		print ("new group created: " + x);*
    
  •  		//add to list as entry x(a float)*
    
  •  		xAxisMap.Add(x, new List<GameObject>());*
    
  •  		xAxisMap[x].Add(lines[cnt]);*
    
  •  	}*
    
  •  }*
    
  • }*

  • void OnGUI()*

  • {*

  •  foreach(KeyValuePair<float, List<GameObject>> line in xAxisMap)*
    
  •  {*
    
  •  	Debug.Log(line.Key + "=" + line.Value.Count);*
    
  •  	if(line.Value.Count > 1)*
    
  •  	{*
    
  •  		GUI.TextArea(new Rect(0,0,100,50), line.Value.Count.ToString());*
    
  •  	}*
    
  •  }*
    
  • }*
    }
    So now that I have posted the full code, this is what happens when I run it:
    I am trying to draw lines with cubes then store their positions. So I add the first cube, the Debug’s print this way:
    New Line Added |
    Looping Through Lines |
    New group created: -9.384 |
    -9.384 = 1
    Then I go to add another line, and no matter where I put it, even if on a different x axis I get this from the debugs:
    Matching group found for value -9.384 |
    existing line found that matches this position, no line will be added. |
    So it seems to think the positions match, even though they dont :confused:

Unity actually checks Vector3 equality by distance: two Vector3 are considered equal when the distance between them is about 1 micro unit or less. If you want to check if some object was already added to the list, Vector3 equality should work. If all objects are considered to be already in the list (even those that aren’t) your code must have another problem elsewhere.

Working code:

	public void BuildLineGroups(GameObject line)
	{

			print ("Looping through lines");
			var x = line.transform.position.x;
			
			//Check to see if there is already an entry for x
			//if there is, then it returns true otherwise its false
			List<GameObject> myList;
			bool wasFound = xAxisMap.TryGetValue(x, out myList);
			
			if(wasFound)
			{
				print ("line added to existing group" + x);
				//otherwise, add this specific cube "line" to that group.
				xAxisMap[x].Add(line);
			}

			
			//if no group with this position was found..
			if(!wasFound)
			{
				print ("new group created: " + x);
				//add to list as entry x(a float)
				xAxisMap.Add(x, new List<GameObject>());
				xAxisMap[x].Add(line);
			}
	}