0
votes

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.

1
What is this script attached to in the inspector? Your problem isn't too clear, is it that the method is being run multiple times and the values being overwritten?Martin Dawson
This script is attached to the prefab; so when I instantiate the entities, each one will have this script. The issue, from my understanding, is that I have N entities, with N scripts and each obviously is calling the update script on their own script, which cause the data to be overwritten. I am trying to trigger the click only on the unit that I click on, but it doesn't happen.user393267
You should put your Raycast script on something else, a single controller type gameobject probably.Gunnar B.
Oh, so that's what it is? Each update is collecting the mouse coordinates through the ray? I thought that once that the first update happens, the mouse is not pressed anymore so it should not catch any other entity.user393267

1 Answers

0
votes

So, each time the data is getting overwritten because you have the script on each prefab.

You don't need this script to be attached to each instance, this code can be on any other single instance game object and would fire just once. Put this script on a GameManager gameObject or something that only has one instance of.

To get the current gameobject only, and not all of them:

// using this to select entities
if (Input.GetMouseButtonDown(0) && Camera.main.name == "top_Camera")
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;

    if (Physics.Raycast(ray, out hit, raycast_length) && hit.collider.gameObject.tag == "npc_entity")
    {
        selected_target = hit.collider.gameObject;
        Debug.Log(selected_target.name + " " + selected_target.GetComponent<AIPlayer>().connected_player.playername);
        UpdateNPCUI();
    }
}