Comparing tags stored in array with the tag of GameObject (Raycasting)

Hey guys am creating an array which is used for storing the tags of all the elements that are currently present on the screen.This array is of string type.Now i want to compare this with the tag of gameobject that is being touched.Am able to save the tags in one script,access it in other script.So just want to know how to compare.Here is the code snipet of what i have done so far…This is the first Script TestScriptJavaScript.The if loop is written in the Update().

var tagsarray=new Array(String);
if (Input.GetMouseButtonDown(0)) 
   	{
 		var ray:Ray;
		var raycast:RaycastHit;
		ray=Camera.main.ScreenPointToRay(Input.mousePosition);
		if (Physics.Raycast (ray, raycast)) 
		{
			switch( raycast.collider.name)
			{
				case "Element_Plane1":
					count++;
					var elementpos=FirstElement.transform.position;
					elementpos.z=elementpos.z-2.0f;
					elementpos.y=elementpos.y-15.0f;
					elementpos.x=elementpos.x-11.0f;
					//FirstElement.renderer.enabled=false;
					var go:GameObject;
					go = Instantiate(Resources.Load("Sphere_Element"),elementpos,Quaternion.identity);
					go.AddComponent("Touchtestscript");
					go.tag="sphere1";
					testtext.GetComponent(TextMesh).text = go.tag;
					tagsarray.Add("sphere1");
					print ("Sphere 1 tag ::: "+go.tag);
				break;
				case "Element_Plane2":
					count++;
					var elementpos1=FirstElement.transform.position;
					elementpos1.z=elementpos1.z-2.0f;
					elementpos1.y=elementpos1.y-15.0f;
					elementpos1.x=elementpos1.x-2.0f;
					//SecondElement.renderer.enabled=false;
					var go1:GameObject;
					go1 = Instantiate(Resources.Load("Sphere_Element"),elementpos,Quaternion.identity);
					go1.AddComponent("Touchtestscript");
					go1.tag="sphere2";
					testtext.GetComponent(TextMesh).text = go1.tag;
					print ("Sphere 2 tag ::: "+go1.tag);
					tagsarray.Add("sphere2");
				break;
				case "Element_Plane3":
					count++;
					var elementpos2=FirstElement.transform.position;
					elementpos2.z=elementpos2.z-2.0f;
					elementpos2.y=elementpos2.y-15.0f;
					elementpos2.x=elementpos2.x+10.0f;
					//FirstElement.renderer.enabled=false;
					var go2:GameObject;
					go2 = Instantiate(Resources.Load("Sphere_Element"),elementpos,Quaternion.identity);
					go2.AddComponent("Touchtestscript");
					go2.tag="sphere3";
					testtext.GetComponent(TextMesh).text = go2.tag;
					print ("Sphere 3 tag ::: "+go2.tag);
					tagsarray.Add("sphere3");
				break;
			}
		}
	}

Now the other script where i am accessing this array is Touchtestscript.And the code in this script is as below.

var testscript:TestScriptJavaScript;
function Start()
{
	testscript=FindObjectOfType(typeof(TestScriptJavaScript));
}
function Update () 
{
	var length=testscript.tagsarray.length;
	print(length);
	var tags;

	for (var touch : Touch in Input.touches) 
	{
 		var ray:Ray;
		var raycast:RaycastHit;
		ray=Camera.main.ScreenPointToRay(Input.mousePosition);
		if (Physics.Raycast (ray, raycast)) 
		{
			for(tags in testscript.tagsarray)
			{
				print("whooaaaa inside found");
				if(raycast.collider.tag.Equals(tags))
				{
					print("whooaaaa inside found");
				}
			}

}
}

So how to do it.Where i am mistaking. Using this code it is not getting in the for loop at all. Not printing the statement.

Hi sanks,

Your whole approach is a bit off.

  1. Adding the touch script to every sphere is wrong. You should have one script that manages touches, and sends messages to GameObjects ( or directly calls methods on scripts).

  2. I strongly advise you to use List and not Array. Lists are faster, and Array is not supported on iOS. List needs System.Collections.Generic.

  3. If every sphere prefab has it’s own tag, you can just check against the tags without referencing them when instantiating.

  4. Why load your prefabs from resources? It is much slower than assigning a prefab to a public GameObject in the inspector.