I am debugging this, but so far I am not really getting anywhere.
My setup: I have prefabs spawned at runtime; they all bear the tag "npc_entity" to identify them. When I click on one of them, I want to get the data which is in one of the components attached to the gameobject (my custom entity class). I achieve this with raycasting in update function, getting the gameobject at hit point.
Now here is the weirdness: I use raycast to get the click on a specific entity in the game window; and read the parameters. IF there is only one entity instantiated, all is OK; but if there are 2 or more, the update is called multiple times, which result in the data being overwritten every time.
I have a script with the update function that does raycasting on the prefab itself; this is the code:
void Update()
{
// using this to select entities
if (Input.GetMouseButtonDown(0) && Camera.main.name == "top_Camera")
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, raycast_length) && hit.collider.gameObject.tag == "npc_entity")
{
selected_target = gameObject;
Debug.Log(selected_target.name + " " + selected_target.GetComponent<AIPlayer>().connected_player.playername);
UpdateNPCUI();
}
}
}
void UpdateNPCUI()
{
ui_manager.NPCpanel.GetComponentInChildren<CanvasGrou>().alpha = 1;
NPC_name.text = selected_target.GetComponent<AIPlayer>().connected_player.playername);
}
What I do not understand is why do I get the behavior, where all the gameobjects instantiated are basically processed. I click once on the gameobject, and the raycast show that the position is consistent with where the gameobject is; the others are nowhere near it.
Any suggestion would be appreciated; even doing step by step debugging; I can't see the issue.