Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by spiningit · Mar 16, 2016 at 01:22 PM · raycastscript.getcomponentrendererchild object

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);
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Mar 16, 2016 at 09:16 PM 0
Share

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

avatar image spiningit hexagonius · Mar 17, 2016 at 08:02 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

63 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Effecting other game objects remotely 1 Answer

How can i get the height of an object 2 Answers

Animation not working when Camera is attached to script 0 Answers

Unity GetComponent Script? 1 Answer

Edit bool on another script and object. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges