My problem occurs when I touch the child object (it's a pillow on a sofa).
collider.transform.name
always returns the parent's name. I'm trying to change the selected object color. The color change script works well except that it always returns the parent.
An example: I want to change only the pillow color on the sofa but it changes the sofa color and not the pillows. I'm new to Unity.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class objectSelect : MonoBehaviour {
public Text tex;
public Text tex2;
public Transform obj1;
public string rr;
void Start () {
}
void Update () {
Touch touch = Input.touches [0];
Vector3 pos = touch.position;
if (Input.touchCount > 0 && touch.phase == TouchPhase.Began) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (pos);
Vector3 planePoint = ray.GetPoint(0.0f);
if (Physics.Raycast(ray,out hit))
{
obj1 = hit.transform;
tex.text = obj1.transform.gameObject.GetInstanceID().ToString();
tex2.text = hit.collider.transform.name;
RR=obj1.transform.gameObject.GetInstanceID().ToString();
}
}
}
public string getselectedObj() {
return obj1.transform.gameObject.GetInstanceID().ToString();
}
public string r() {
return "rrrrrR";
}
public string RR {
get {
return rr;
}
set {
rr = value;
}
}
}
The Box Collider of the sofa contains both the pillow colliders. As a result, no ray or in fact no object can touch the pillow collider, because it is inside the sofa collider, which is why the sofa collider will always be touched first. The solution to this can be to make a compound collider for the sofa, that is make one collider for the base of the sofa, add it to the "sofa" game object, make two-three more colliders for the sofa's armrests and backrests and add them as children of the collider.