2 same Scripts w/ GetComponent : one works, one doesn't
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);
I'm wondering how the first script works. UI elements do not use a Renderer but either an Image, or it's parent class $$anonymous$$askedGraphic, or it's parent Graphic. Don't know how to toggle that though
hi, would you $$anonymous$$d telling me why you think i use UI elements? maybe something not clear in my question and i want to clarify it. because i want to activate Render of an object which is parented to another object. so i'm not using UI element except for the android button
Answer by spiningit · Mar 17, 2016 at 09:10 AM
I just found the solution for this problem. I commented out this line and now it works.
foreach (Transform child in transform){
}
but I don't know why it caused the GetComponent ().enable not working. can someone maybe explain?
Answer by FunFuel · Apr 30, 2018 at 11:29 AM
Hello! Just got into a problem when I couldn't GetComponent() from Transfrom component of the same object, while it was INACTIVE.
Transform expandingGroupTransform = this.gameObject.transform.GetChild(1);
Debug.Log (expandingGroupTransform.gameObject.name);
GameObject expandingGroup = expandingGroupTransform.GetComponent<GameObject> ();
Debug.Log (expandingGroup.name);
First Debug.Log was showing the right name of object, while second was not showing anything. I just wrote next thing and it helped:
Transform expandingGroupTransform = this.gameObject.transform.GetChild(1);
Debug.Log (expandingGroupTransform.gameObject.name);
GameObject expandingGroup = expandingGroupTransform.gameObject;
Debug.Log (expandingGroup.name);
Both Debug.Logs showed correct name. Looks like Transform component already holds GameObject component and we can scip GetComponent() part.
Your answer
Follow this Question
Related Questions
Get component from parent of children hittet with raycast 1 Answer
Storing Renderer in a variable not working! 1 Answer
profile is very high (script & render) :( 1 Answer
GetComponent returning null with no errors 1 Answer
Renderer error 0 Answers