Search Unity

How to find tagged elements in found prefab?

Discussion in 'Scripting' started by Slyrfecso1, Sep 26, 2015.

  1. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    Hi,

    I have some gameobjects on the scene and if I click on one, then that is stored.
    After I'd like to find the tagged elements in this game object and I'd like to change all materials
    to other, only on tagged elements.

    1.
    I can find and store the clicked object.
    //This is on "Player" layer.

    2.
    I can change the materials on tagged layer, but this is changing on all game object on the scene.
    //Material elements are on "Butorlap Objects List" layer

    3.
    I'd like to change the materials of the tagged elements in one object.

    Any help will be helpful.

    Code (CSharp):
    1. Ray ray;
    2. RaycastHit hit;
    3. public GameObject objects;
    4.  
    5. //...
    6.  
    7.  
    8. if ( Input.GetMouseButtonDown(0))
    9.         {
    10.             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    11.             if(Physics.Raycast(ray, out hit))
    12.             {
    13.                 if(hit.collider.tag == "Player")
    14.                 {  
    15.                     objects = hit.collider.gameObject;
    16.                 }
    17.             }
    18.         }
    19.  
    20. //...
    21.  
    22.  
    23. foreach(GameObject termek in GameObject.FindGameObjectsWithTag("Butorlap Objects List"))
    24. {
    25. Renderer[] rendererComponents = termek.GetComponentsInChildren<Renderer>();
    26. foreach (Renderer component in rendererComponents)
    27. {
    28. int index = inventory[i].butorlapID;
    29. component.GetComponent<Renderer>().material.mainTexture = texturesButorlap[index];
    30. }
    31. }
     
    Last edited: Sep 26, 2015
  2. Thaao

    Thaao

    Joined:
    Jun 9, 2014
    Posts:
    54
    Instead of trying to find objects with the tag, why not just grab the renderer of all the children of the object you hit with your raycast, then only change their information if they have the correct tag?

    Here's an example:

    Code (CSharp):
    1. Renderer[] rendererComponents = objects.GetComponentsInChildren<Renderer>();
    2. foreach (Renderer component in rendererComponents)
    3. {
    4.     if(component.tag == "Butorlap Objects List")
    5.     {
    6.         //do stuff
    7.     }
    8. }
    Using FindGameObjectsWithTag is always going to find ALL the GameObjects in the scene with that tag.
     
    Kiwasi likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You probably also want to climb up the hierarchy first, before traversing down it. Otherwise clicking on a non parent collider will only change it's selection.
     
  4. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    Thaao your solution is fantastic, but I have problem with selection. When my wood inventory is over the game object and when I click one, then under the icon is selecting the object and for this object would be change the material not for the other on the scene.

    How can I remove/desable hit on Inventory icons?
    You could see my screen record, if you have any idea...
    http://terberendezo.alexbutor.hu/Selection_problem.mov
     
    Last edited: Sep 28, 2015
  5. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    Hi,

    I have found EventSystem solution, but it isn't working.
    I have Inventory icons and I'd like to disable the pass trough click.
    How can I add Inventory icons to EventSystem?
    (texturesButorlap[index] or slots[index]) ???


    Code (CSharp):
    1. using UnityEngine.EventSystems;
    2.  
    3. ...
    4.  
    5. void Update()
    6.     {
    7. if ( Input.GetMouseButtonDown(0))
    8.             {
    9.             if(EventSystem.current.IsPointerOverGameObject(-1)) return;
    10.            
    11.                 ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    12.                 if(Physics.Raycast(ray, out hit))
    13.                 {
    14.                     if(hit.collider.tag == "Player")
    15.                     {  
    16.                         objects = hit.collider.gameObject;
    17.                     }
    18.                 }
    19.  
    20.         }
    21. }
     
    Last edited: Sep 28, 2015