So I have a part of the script here since it's long. I want to add shift sounds when the player goes up or down a gear, problem is I don't know how to make it play. Here's a part of it Code (CSharp): void ShiftGears() { //Shift Down if(currentSpeed >= gearSpeeds[currentGear] && FL_WheelCollider.rpm > 0) { currentGear ++; } //Shift Up if (currentSpeed < gearSpeeds[currentGear - 1]) { currentGear--; } EngineSound(); }
Create a public AudioSource variable: Code (CSharp): public AudioSource gearShiftAudioSource; Create an AudioSource in the Editor and make it a child of the car. Assign the gear shift sound clip to the AudioSource then drag and drop the AudioSource in the newly created "gearShiftAudioSource" slot in the inspector. Other ways to go about this is to have a function that creates the AudioSource at runtime. In the ShiftGears() function, Play the AudioSource: Code (CSharp): if(currentSpeed >= gearSpeeds[currentGear] && FL_WheelCollider.rpm > 0) { currentGear ++; gearShiftAudioSource.Play(); } if (currentSpeed < gearSpeeds[currentGear - 1]) { currentGear--; gearShiftAudioSource.Play(); }
Did you drag your audio clip into the AudioClip field on the AudioSource component? Did you drag your AudioSource into the public field on your script? https://docs.unity3d.com/Manual/class-AudioSource.html
I can't find it in the inspector. Even when I went back to add the clips and audiosource, the button didn't show up in the inspector. Would I have to restart all of my cars so I could get the button to show up or would I need to edit it with something else cause I'm using Notepad instead of Monoeditor.
So I used VC and it couldn't find the varible. So I went in and wanted to do this Code (CSharp): //Shift Down if (currentSpeed >= gearSpeeds[currentGear] && FL_WheelCollider.rpm > 0) { currentGear++; NewMethod(); } if (currentSpeed < gearSpeeds[currentGear - 1]) { currentGear--; NewMethod1(); }