2

I have spent all day looking for a solution to this problem, and I simply can't find one. Using JavaScript in Unity 3D, I have a script where I want to play a sound when the player's velocity on the X axis reaches a certain point, and if it's not at that point, then the sound will be muted. And I believe I have all the structure right, it's just the line of code that says to mute the audio that won't work. I've tried all kinds of different combinations, and I get an error for each one.

The script looks like this:

#pragma strict

var playing = false;
var audioSource = GetComponent.<AudioSource>();

function Update () {
    if (transform.GetComponent.<Rigidbody>().velocity.x <= 2.5 && 
transform.GetComponent.<Rigidbody>().velocity.x >= -2.5)
    {
        Mute();
    } else {
        Unmute();
    }
}

function Mute () {
    audioSource.mute = true;
}

function Unmute () {
    audioSource.mute = false;
    Sound();
}

function Sound () {
    if (transform.GetComponent.<Rigidbody>().velocity.x >= 2.5 && playing == 
false)
    {
        playing = true;
        GetComponent.<AudioSource>().Play();
        yield WaitForSeconds(2);
        playing = false;
    }
        if (transform.GetComponent.<Rigidbody>().velocity.x <= -2.5 && 
playing == false)
    {
        playing = true;
        GetComponent.<AudioSource>().Play();
        yield WaitForSeconds(2);
        playing = false;
    }
}

I've gotten all kinds of different errors, but the one I seem to be getting the most says "UnityException: GetComponentFastPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'motioncheck' on game object 'Ball'." I'm not sure what this means, since I'm still kinda a nub at JavaScript.

I feel like it shouldn't be this hard to just mute a sound. I'm going to assume that the answer to this is really simple and that I'm just really dumb. That's what usually seems to happen, lol.

In the mean time, I'm going to continue my rampage across the internet in search for answers to this problem.

Ruzihm
  • 19,749
  • 5
  • 36
  • 48

1 Answers1

0

Your mute code is fine.

"UnityException: GetComponentFastPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'motioncheck' on game object 'Ball'." I'm not sure what this means, since I'm still kinda a nub at JavaScript.

See this:

var audioSource = GetComponent.<AudioSource>();

That's a Unity API and you have to call their functions from inside a function. The Awake or Start function is appropriate for initializing component variables.

var audioSource : AudioSource;
function Start() 
{
    audioSource = GetComponent.<AudioSource>();
}

Note that Unityscript/Javascript is now discontinued. They no longer update the doc on this and you cannot create new scripts from the Editor anymore. It still works as for now but the compiler will be removed soon. Please learn and start using C# before its support is totally removed.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thank you so much! It works perfectly now. I wasn't aware that GetComponent was only available in the Start or Awake function, but, now I know! And yes, I will try to learn C#. I knew Unity suddenly got rid of a bunch of JavaScript features, but I didn't know that they planned on removing it. From what I've hear, C# is a lot like JavaScript, so it shouldn't be too hard. –  Aug 01 '18 at 18:05
  • Just few changes but it's no hard to learn. Glad I was able to help – Programmer Aug 01 '18 at 18:07
  • @EthanWaldeck Additionally, `yield WaitForSeconds(2);` won't (or shouldn't) work outside of a coroutine. – Draco18s no longer trusts SE Aug 01 '18 at 20:43
  • @Draco18s Hmm. . . it seems to work perfectly fine for me. Actually, I put that line of code inside of a different function, that isn't a pre-built function, specifically because I got that error when I tried to put it inside of the Update function. –  Aug 02 '18 at 01:03
  • @EthanWaldeck I think that Draco18s is extending my comment and telling you the changes or difference you need to make when you change to C#. – Programmer Aug 02 '18 at 02:06
  • @Programmer Oh. I see. I will make sure I keep note of that then. Thanks! –  Aug 03 '18 at 18:25