If for some reason the collider on that landing pad is what is triggering the guns to shoot then I would investigate that and determine why, it shouldn’t happen so perhaps it needs a bit of code wrapped around whatever is there are the moment to prevent the ship from firing when it’s either landed, landing, and/or the player no longer has control of the ship.
That makes sense, typically you would get these references before any of your other code executes, so using the Awake
or Start
method makes perfect sense, so your code here;
private void Start()
{
playAudio = gameObject.GetComponent<AudioSource>();
}
…is fine, again, I’d raise the issue of the naming of the variable but getting the component at this point in your code is good.
However, getting it again here;
private void SetAudioSourceActive(bool isActive)
{
playAudio = gameObject.GetComponent<AudioSource>();
playAudio.enabled = isActive;
}
…then becomes unnecessary, e.g. there is no need to get it twice, all you are doing is overwriting the first reference with the second, which is actually the same thing.
It would, because of the second reference above, typically though you will find it much easier to have the getting of components, or finding of objects in these Awake
/Start
methods, and as you keep developing this is typically where you yourself would look for them.
You do, of course, only need to have this reference as a member variable if it is actually going to be used across multiple methods, or serve some other purpose within the class, or, if it removes the repetitiveness of getting it.
So, if we look at what your code currently does in layman’s terms, leaving the GetComponent
call in the SetAudioSourceActive
method;
- Update gets called every frame
- Every time Update is called you check to see if
isActive
is true, it always will be because you’ve set it to be when declaring the member variable.
-
ProcessFiring
is then called, every frame, regardless of the isActive
variable, as nothing else changes it
-
ProcessFiring
checks to see if the fire button is being pressed, in either case, you call SetAudioSourceActive
which calls the GetComponent
method
There is no value in calling the GetComponent
method every single frame, its redundant and costs you in performance.
The isActive
member variable you have declared is also not providing any benefit, you could remove that, and the if
statement within the Update
method and just literally call ProcessFiring
.
I’m not sure why you want to set the AudioSource.loop property to either true/false, I would have though that when the player presses fire you want to play an audio clip just the once?
I’m also not sure why you want to enabled/disable the AudioSource within the SetAudioSourceActive
method?
Somethings you could try, one at a time;
-
put the Start
method back in, and put the GetComponent
code back in, then comment out this line from the SetAudioSourceActive
method;
playAudio = gameObject.GetComponent<AudioSource>();
Run the game, it should still work as it did before, but now it’s not getting the component over and over again for no reason.
-
Within the Update
method, comment out the two lines of code which set the AudioSource.loop property to either true/false
Run the game, do you notice any difference in the sound?
Assuming not, you could delete those two lines.
-
Within the SetAudioSourceActive
method, comment out the following line;
playAudio.enabled = isActive;
Run the game, does anything adverse happen, or is everything still working as expected?
Assuming all is well, that leaves the SetAudioSourceActive
method with two commented out lines of code, which means it is now not doing anything.
You could then remove the method and the two statements in the Update
method which call it.
If the sound effect wasn’t heard, that will be because you are relying on Play on Awake on the AudioSource component, if you disable this, and instead call the AudioSource’s Play
method within ProcessFiring
it should be heard again.
The above would strip out a lot of unnecessary code, increase the performance of your code, and still leave you with the sound effect being controlled via this script.
With all of the above, make the changes one at a time and review, don’t do them all, then post “It didn’t work”. Instead, make each change and let me know what the effect of that change was, in the case of it being ok and no problems, move on to the next change etc.