Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by Crystalline · Oct 31, 2013 at 11:53 AM · javascripterrorsoundwarning

Implicit Downcast warning.

Hello everyone.

Since a while I get this warning

Assets/#Scripts/Player.js(32,35): BCW0028: WARNING: Implicit downcast from 'UnityEngine.Component' to 'UnityEngine.AudioSource'.

And here is my code:

 private var SND_toggle_item :AudioSource;
 
 function Start()
 {
 
 // audio sources
 var factory = gameObject.GetComponents(AudioSource);
 SND_toggle_item = factory[6];
 
 }

What do I need to change in order to get rid of this warning?

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
3
Best Answer

Answer by vexe · Oct 31, 2013 at 11:55 AM

You could explicitly downcast it, if you wanna get rid of that warning.

Instead of casting the thing, just use the generic explicit type-defining GetComponent, like:

 var factory : AudioSource[] = gameObject.GetComponents.<AudioSource>();

You shouldn't need to cast, after that point.

GetComponents return a Component[] - You're storing that in your factory variable - now factory is a Component[] - In your next line, you're assigning factory[6] which is a Component to SND_toggle_item which is an AudioSource - which inherits Behaviour, which in turn inherits Component. In other words, an AudioSource is a specific type of Component. So in your assignment, you're trying to assign a Component to an AudioSource - Component is higher in the inheritance tree, than AudioSource - In C#, that assignment would give you an error, in JS it appears it's giving just a warning (Another reason I don't like JS) - It appears it's automatically (implicitly) doing the downcast, meaning it's down-casting factory[6] to an AudioSource to reach equivalency and type-match in your assignment. (Remember, when you assign Y to X, there has to be a type-match between them, otherwise the assignment is invalid)

I hope it's clear now.

Comment
Add comment · Show 4 · 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 Crystalline · Oct 31, 2013 at 11:57 AM 1
Share

Sorry to bother again,but that line of code gives me errors.. (expecting,unexpected token)

avatar image vexe · Oct 31, 2013 at 12:26 PM 4
Share

Apologies, I'm no JS coder. Just add a '.' before the '

avatar image Crystalline · Oct 31, 2013 at 12:28 PM 0
Share

Thanks for explanation! I managed to fix these errors by using something more related to what I actually needed. .PlayOneShot().

But with you line I fixed my other scripts. Thanks again.

avatar image cregox · Dec 12, 2014 at 10:10 AM 0
Share

You mean before the "lesser than" character <

Answers comments ate it.

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

16 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

Related Questions

Sound system not working correctly 0 Answers

'Play' is not a member of 'Object' 1 Answer

Issue with OnTriggerEnter. 1 Answer

expecting (, found 'Update' and unexpected char: 0x200B. 2 Answers

Player not taking any damage 2 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