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 /
  • Help Room /
avatar image
0
Question by Hlero · Feb 23, 2017 at 10:57 AM · c#audiosourceplayoneshot

Found a weird problem about AudioSource.PlayOneShot

AudioSource _AudioSource_1 = new GameObject().GetComponent<AudioSource>();
AudioSource _AudioSource_2 = null;
Debug.Log(_AudioSource_1.Equals(_AudioSource_2));
_AudioSource_1.PlayOneShot(new AudioClip());
Debug.Log("Pass1");
_AudioSource_2.PlayOneShot(new AudioClip());
Debug.Log("Pass2");

Console
Here is my code and what console displayed.
I'd appreciate it if someone could tell me why this
_AudioSource_1.PlayOneShot(new AudioClip()); can pass.

88655-playoneshot.png (11.2 kB)
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 IgorAherne · Feb 23, 2017 at 12:51 PM 0
Share

console + image cannot be displayed, revise your question

avatar image Hlero IgorAherne · Feb 23, 2017 at 01:53 PM 0
Share

Revised.Can you see it now?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Feb 23, 2017 at 07:50 PM

I know that unity overloaded null, maybe equals also, for several reasons. you are, somehow, successfully using the constructor on the returned AudioSource. maybe it creates a new instance but overrides null so that it's not pointing to any legal C++ native component anymore effectively comparing nulls.
don't use new on any unity class. use AddComponent if you need another.

Comment
Add comment · Show 1 · 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 Hlero · Feb 24, 2017 at 07:16 AM 0
Share

Thanks a lot! That makes sense to me. By the way, AudioClip is derived from Object, it can't be added to a GameObject.

avatar image
0

Answer by Hlero · Feb 24, 2017 at 08:29 AM

I did some research and found, truly like @hexagonius said, unity overrided something like Equals, == and != to build a Null Object pattern.
So here is my code to test:

AudioSource _AudioSource_1 = new GameObject().GetComponent<AudioSource>();
Debug.Log(object.Equals(_AudioSource_1,  
FormatterServices.GetUninitializedObject(typeof(AudioSource))));
Debug.Log(_AudioSource_1.GetType());
Debug.Log(_AudioSource_1);
_AudioSource_1.PlayOneShot(null);
Debug.Log("Pass1");

And Console:
alt text
It looks like GetComponent<T> will always return reference of a T type instance if T is a NATIVE component type, whether or not there is a T component attached to the gameobject.

But when you want to get a custom component, if there is no one attached, GetComponent will definitely return a real null. Like this:

GameDataState _GameDataState = new GameObject().GetComponent<GameDataState>();
Debug.Log(object.Equals(_GameDataState, null));
Debug.Log(_GameDataState.enabled);
Debug.Log("Pass");

alt text
It would yield a NullReferenceException.
So you can see unity overrided some method to make you think it's null when you try to get a not attached component, and i think maybe they forgot to deal with this PlayOneShot method so it can be invoked.


nulltest.png (13.9 kB)
customecomponent.png (4.8 kB)
Comment
Add comment · Show 2 · 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 hexagonius · Feb 24, 2017 at 10:09 AM 0
Share

it's just plain wrong to use new here. A GameObject must not be created through it's constructor. and now that I read more careful it's just that you call GetComponent on a new gameobject that simply returns null because there's no such component. So it's not even some strange overload, it's simply what GetComponent does when there's no component.

avatar image Hlero hexagonius · Feb 24, 2017 at 10:44 AM 0
Share

I use new just for test and i have tried to create a gameobject through inspector and find it, then i called GetComponent<AudioSource>().PlayOneShot(null), the result was same. Look the question my friend! $$anonymous$$y problem is when i call GetComponent<AudioSource> on a gameobject without AudioSource, it returns me a reference of an AudioSource instance, and i can call PlayOneShot method of it without any exception. It is supposed to be a NullReferenceException but it's not! That's what confused me ok?

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

315 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 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

AudioSource.PlayOneShot with negative pitch won't start new sounds 0 Answers

Help to play music whenever the character enters a GameObject... 1 Answer

Second AudioClip won't play 0 Answers

How to make an editor extension to unity ui for switching audio clips? 0 Answers

AudioSource.Play plays every sound at once,AudioSource.Play Plays every source at once 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