Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by richwarp · Sep 26, 2019 at 10:18 AM · scripting problem

Using GetComponentsInChildren to detect renderer

I am trying to modify a script that detects if an object is in direct view, and if so, to crossfade between 2 audio sources. The code is below:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Audio;

public class CameraAudioCtrl2 : MonoBehaviour {

 public Transform playerCamera;
 public Transform audioObject;
 public AudioSource unfiltered;
 public AudioSource filtered;
 public float distanceThreshold;
 public float distance;
 public float rampTimeGrain;
  


 // Start is called before the first frame update
 void Start()
 {
     renderer = GetComponentsInChildren<Transform>();
 }

 // Update is called once per frame
 void Update()
 {
     
    distance = Vector3.Distance(playerCamera.position, audioObject.position);

    if(audioObject.GetComponent<Renderer>().isVisible && distance < distanceThreshold)
    {
             if(unfiltered.volume > 0.0f)
                 unfiltered.volume -= rampTimeGrain;
             if(filtered.volume < 1.01f)
                 filtered.volume += rampTimeGrain;
    }

    else
    {
             if(unfiltered.volume < 1.0f)
                 unfiltered.volume += rampTimeGrain;
             if(filtered.volume > 0.0f)
                 filtered.volume -= rampTimeGrain;
    }

 }

}

The script is attached to the main camera, and audioObject is the game object that has a renderer attached. I was advised to use GetComponentsInChildren ([presumably because the game objects are prefabs), but when I run the script I am told the game object does not have a renderer attached to it. I'm fairly new at scripting so I'm probably missing something obvious. Does anyone have an idea of what I might be doing wrong here?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Mouton · Sep 26, 2019 at 01:06 PM

In your Start event, you are retrieving Transform components and not Renderer component. On top of that, you are using the method GetComponentsInChildren, which is plural and will return an array of components, not a single component. Use the GetComponentInChildren (without the "s") instead.

 void Start()
 {
     renderer = GetComponentInChildren<Renderer>();
 }
Comment
Add comment · Show 5 · 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 richwarp · Sep 26, 2019 at 02:05 PM 0
Share

Thank you! I hadn't meant to put Transform in there actually. I'll try the singular approach for the GetComponent call.

avatar image richwarp · Sep 26, 2019 at 02:16 PM 0
Share

Now when changing to the above I get this error:

'Component.renderer' is obsolete: 'Property renderer has been deprecated. Use GetComponent() ins$$anonymous$$d. (UnityUpgradable)'

avatar image Mouton richwarp · Sep 26, 2019 at 05:12 PM 0
Share

Use $$anonymous$$eshRenderer type and rename your variable meshRenderer ins$$anonymous$$d:

 void Start()
 {
     meshRenderer = GetComponentInChildren<$$anonymous$$eshRenderer>();
 }
avatar image richwarp richwarp · Sep 27, 2019 at 12:14 AM 0
Share

I took a slightly different approach in the end, creating a totally new game object with a renderer attached, and referencing that in the script ins$$anonymous$$d. But now I have a different problem. It appears that I cannot enact this same behavior on two separate objects in the scene. If I attach 2 instances of the script to the main camera, and point them at different game objects, only one of them triggers the audio behavior at any given time. I have switched one of the two scripts off to validate this and sure enough, when only one instance is enabled it works fine. Is there a solution to this?

avatar image Mouton richwarp · Sep 27, 2019 at 06:41 AM 0
Share

The fact that it does not work with 2 instances of the script is because of the if/else condition in the for loop: each script check the condition and change the filtered/unfiltered values, thus only the second script in the execution loop will change the value.

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

196 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how do i use component gathered from ontriggerEnter, outside of that function? 2 Answers

How to set a limit for my player rotation ? 0 Answers

It is posible to attach a class script, that inherits from MonoBehaviour, to an estate from the state machine? 0 Answers

Change position of instantiated parent's children 1 Answer

IJobParralelFor giving me random result. 0 Answers


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