A bunch of units, how to handle raycasting?

Okay, so imagine the following scenario → I have 10 tanks on the grounds, the same type units. My first idea was to add a script to that tank type prefab, which waits and reacts when user clicks on it and from then on… That way all of my tanks would have a code for them selfs, running in the background, and it would be all tidy and clean, not a single C# code with thousands if statements for raycast checking…

BUT, I can’t use tags… :confused: I mean, if I have basically the tag “tank_type_01”, and check raycast against that tag, when I click on that single unit, all of the corresponding tanks (since they’re all prefabs, and all are of type tank_type_01) would react as well… :confused: How can I control if the user has clicked just on that particular tank, what are other options except tags? Is there something like “if (physics…) if (this boxcolider)…” since each tank has box colider on it?

Thanks!

Ok, so I think I got this worked out, it’s in C# though, hopefully this isn’t an issue.

using UnityEngine;
using System.Collections;

public class rayCast : MonoBehaviour
{
	public Camera cam;
	public Ray ray;
	public RaycastHit hit;
	public Collider collider;

	void Start()
	{
		
	}
	
	void Update()
	{
		ray = cam.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit = new RaycastHit();
		if(Physics.Raycast(ray, out hit, 100f))
		{
			collider = hit.collider;
		    
		}
	}
}

Basically just attach the main camera into the ‘cam’ variable on this script. Currently all that is happening is the collider is set when the mouse hovers over an object. You can see this happening in the editor, via the ‘collider’ variable. I didn’t put any button checking, or any visual indication, because I didn’t know what you would want. I suppose just wrapping the update code inside of a mouse button check would suffice. Hopefully this helps.