I want to make an application for both Standalone version and Android version, in which i’m trying to enable Mesh Renderer of an object attached to its parent object when i press/touch button.
edit:
Sorry if i wasn’t clear. So this is the case, there is an object called Box A which has child box B. The renderer of box B is disabled at start. When raycast from main camera hit the box A, and then i touch the button (in which the script is attached to), the render of box B should be enabled. That’s the idea which runs perfectly in pc-version.
In the pc-version i attach this script in the main camera and it works superb
public class RayCast : MonoBehaviour {
public float rayDistance;
RaycastHit hit;
void Update () {
Debug.DrawRay (this.transform.position, this.transform.forward * rayDistance,
Color.blue);
RaycastHit hit;
if (Physics.Raycast (this.transform.position, this.transform.forward, out
hit, rayDistance, 1<<8)) {
if (Input.GetButtonDown ("Fire1")){
Debug.Log (hit.collider.gameObject.name);
foreach (Transform child in transform){
Renderer renderChild = hit.collider.transform.GetChild(0).GetComponent<Renderer>();
renderChild.gameObject.GetComponent<Renderer>().enabled = true;
}
}
}
}
}
And in the android version I attach at the UI-button a similar script as follow
public class Funktionen : MonoBehaviour {
RaycastHit hit;
public float rayDistance = 5.0f;
private Camera cam;
void Awake () {
cam = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent <Camera>();
}
void Update () {
Debug.DrawRay (cam.transform.position, cam.transform.forward * rayDistance,
Color.blue);
RaycastHit hit;
}
public void Shooting () {
if (Physics.Raycast (cam.transform.position, cam.transform.forward, out
hit, rayDistance, 1<<8)) {
foreach (Transform child in transform){
Renderer renderChild = hit.collider.transform.GetChild(0).GetComponent<Renderer>();
renderChild.gameObject.GetComponent<Renderer>().enabled = true;
}
Debug.Log (hit.collider.gameObject.name);
}
}
}
and this script doesn’t work as i can’t enable Renderer when i touch the button. what strange is, the debug works and it brought me the name of the parent object each time i touch the button.
So what did i miss?
edit:
I added this line in the 2nd script and it brings me the name of the child. but why can’t i enable the renderer?
Debug.Log (hit.collider.transform.GetChild(0).name);