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 Hogge · Apr 03, 2017 at 10:38 PM · audioaudioclip

Change audio clip through code

I'm currently creating a flight game and today I decided to implement simple engine sounds. Changing the pitch worked fine, but when I decided to implement a different sound for when the thrust is high and low. The code's simple:

     if (thrust > 1000) {
             Source.pitch = (thrust / 80000);
             audio.clip = EngineSoundMain;
 
         }
         if (thrust < 1000) {
             Source.pitch = 1;
             audio.clip = EngineSoundIdle;
         }


However, when I press play, the game plays... no sound at all. The error log says the following:

NullReferenceException: Object reference not set to an instance of an object PlanePhysics.Update () (at Assets/Scripts/PlanePhysics.cs:78)

Which I find quite baffling. What object am I supposed to refer to? And how?

Comment
Add comment · Show 3
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 Vicarian · Apr 03, 2017 at 11:05 PM 0
Share

@Hogge How have you defined EngineSound$$anonymous$$ain and EngineSoundIdle?

avatar image Hogge Vicarian · Apr 04, 2017 at 12:08 AM 0
Share

Yeah. They're public audioclips.

avatar image Vicarian Hogge · Apr 04, 2017 at 12:27 AM 0
Share

With the audio variable, you're attempting to access the AudioSource attached to the gameObject with the PlanePhysics script, if it's not defined elsewhere. This variable is also deprecated. I suspect Source is the variable you've set public and given a value to? In which case, try using Source, if it has a value. Without seeing the entire source listing, I'm not sure which object is pointed to by line 78, so I'm not sure if audio, Source, or the clips themselves are returning null.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by toddisarockstar · Apr 04, 2017 at 04:10 AM

this is hard to correct cause we can't see the rest of your code and your lookups. but I cant see why you are trying to change the pitch and the clip differently.

actually even if you are not getting errors, if you keep calling it like you are showing you will find you cant keep updating the component, but you do need to keep checking the value. so you need another varible to check if the statis has changed.

this should work:

     public AudioClip somemp3; //<---drag mp3 into the inspector here
          public AudioClip nextmp3; //<---drag  mp3#2 into the inspector here
                   AudioSource audio;
  
  int current;
                
            void Start() { 
          // you need a reference to your component
           audio=gameObject.GetComponent<AudioSource>();
       }
       
       // now you should be able to say this anywhere else in your code
        void Update(){
       if(whatever){
  
            if(current!=0){current=0;
            audio.clip=somemp3;
            audio.pitch=.5f;
   audio.Play();
  }
   
            }else{
  if(current!=1){current=1;
    audio.clip=nextmp3;
            audio.pitch=.8f;
   audio.Play();
  }
   }
  

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 toddisarockstar · Apr 04, 2017 at 05:15 AM 0
Share

also, please don't use division!! it hurts my eyes. lol. it's basic progra$$anonymous$$g knowledge to know that a CPU requires many many many more bits and bytes to do division ins$$anonymous$$d of multiplication.

if you need a smaller percentage multiply by .00008 ins$$anonymous$$d of dividing by 80000 or whatever. sometimes C# can even wrestle with giving you less accurate results for that reason.

Fortunatly, Once you get used to it, there are actually very few circumstances when you truly need division.

avatar image Jigglebizz toddisarockstar · Mar 09, 2020 at 06:08 AM 0
Share

Division doesn't require more bits and bytes. It requires more CPU cycles. Integer/float multiplication takes 2-5 cycles, integer division takes 15-44 cycles, and floating-point division takes 35-38 cycles. This is how long the actual math machines in the processor take to work, and has nothing to do with the amount of memory used.

$$anonymous$$eep in $$anonymous$$d, if you're going to split hairs here, that it takes 3-4 cycles to read from L1 cache and 300 (that's 10x more than division!!!) to read from RA$$anonymous$$. In short, it's way more important to cache your variables and be aware of non-consecutive reads than division vs. multiplication. But a good developer does both.

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

81 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

Related Questions

How to play several audio cllips one after another. 3 Answers

Slowly fade audio's pitch on key press? 3 Answers

Problem with playing sound on Trigger-Enter 2 Answers

How to get decibel's value of game's sound? 0 Answers

Game lags when loading AudioClip from Resources 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