Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Select one Gameobject by click

Discussion in 'Scripting' started by xarapuchatok, Feb 26, 2020.

  1. xarapuchatok

    xarapuchatok

    Joined:
    Dec 27, 2019
    Posts:
    11
    Hi everyone! Need help! I have some gameobjects with same script and when I click on gameobject I select it by tag, so other gameobject selects too, but when I change tags on others gameobjects I have to make changes on each script!
    Is there any solution to select only one gameobject but if I have many prefabs of them on scene?
    Thanks
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    I dont understand your problem... but selecting only one object by click is easy. Raycast through the camera at the position of your mouse and take the first object that was hit (potentially the first having a specific tag). That should be it.
    Or is your problem that you have multiple scripts doing this, so all select the object you clicked on, but you only want one to do so? This would seem like a design flaw. Think about what you really want to happen and implement it like that.
     
  3. xarapuchatok

    xarapuchatok

    Joined:
    Dec 27, 2019
    Posts:
    11
    Thanks for answer! Yes, I hit the object by mouse click at the position in camera script
    script on camera CameraEngine.cs
    Code (CSharp):
    1. public static string[] control_array = new string[2];
    2. If (Input.GetMouseButtonUp(0))
    3. {
    4. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    5. RaycastHit hit;
    6. if(Physics.Raycast(ray, out hit))
    7. {
    8. control_array[0] = hit.collider.tag
    9. }
    10. }
    And script on gameobject
    Code (CSharp):
    1. if (CameraEngine.control_array[0] == "Player")
    2. { //do something
    3. }else{
    4. //do something
    5. }
    So if I hame many gameobjects with the same script how I can sekect only one of them on which I clicked?
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Ok, so the problem here seems to be that you are executing the right idea in a weird way.
    I assume you have different characters and want to control one when you select him. Sort of like in an RTS game.

    If the above understanding is roughly correct, then the whole script you attach to your gameobjects is not necessary. The gameobjects only need a script, let's call it ControllableUnit, which offers functions like MoveTo(Vector3 targetPos). Your camera script (or some other, given that having the controlling code in the camera may be a bit confusing) would then save the hit unit, not its tag. So you use your code, but instead of saving the tag, you save the hit.transform.gameObject.
    Now you know the reference to your unit, and could execute its movement functions.

    The summarized changed would look something like this:

    Code (CSharp):
    1. public static GameObject controlledUnit = null; // instead of GameObject, could use custom type like ControllableUnit
    2. If (Input.GetMouseButtonUp(0))
    3.     {
    4.     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    5.     RaycastHit hit;
    6.     if(Physics.Raycast(ray, out hit)) // you can also only accept hits to some layer and put your selectable units in this layer
    7.     {
    8.         if(hit.collider.tag == "Player"){
    9.             controlledUnit = hit.transform.gameObject; // if using custom type, cast the result to type here
    10.         } else{
    11.            controlledUnit = null;
    12.         }
    13.     }
    14. }
    Now you have access to the controlledUnit gameObject. If you use the approach i posted above, you would probably want to cast it to a ControllableUnit type (your own type where you put methods like MoveTo), in which case you could then simply call controlledUnit.MoveTo(someLocationVector).

    Is this what you are looking to do?
     
    xarapuchatok likes this.
  5. xarapuchatok

    xarapuchatok

    Joined:
    Dec 27, 2019
    Posts:
    11
    Exactly! Thank you very much!