- Home /
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?
@Hogge How have you defined EngineSound$$anonymous$$ain and EngineSoundIdle?
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.
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();
}
}
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.
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
