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.
I can find and store the clicked object.
//This is on “Player” layer.
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
I’d like to change the materials of the tagged elements in one object.
Any help will be helpful.
Ray ray;
RaycastHit hit;
public GameObject objects;
//...
if ( Input.GetMouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
if(hit.collider.tag == "Player")
{
objects = hit.collider.gameObject;
}
}
}
//...
foreach(GameObject termek in GameObject.FindGameObjectsWithTag("Butorlap Objects List"))
{
Renderer[] rendererComponents = termek.GetComponentsInChildren<Renderer>();
foreach (Renderer component in rendererComponents)
{
int index = inventory[i].butorlapID;
component.GetComponent<Renderer>().material.mainTexture = texturesButorlap[index];
}
}
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?
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.
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.
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]) ??? ```csharp *using UnityEngine.EventSystems;
…
void Update()
{
if ( Input.GetMouseButtonDown(0))
{
if(EventSystem.current.IsPointerOverGameObject(-1)) return;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
if(hit.collider.tag == "Player")
{
objects = hit.collider.gameObject;
}
}
}