Audio Issue

Updated on January 27, 2016 in [A] Audio
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
14 on January 26, 2016

[Your title has been changed, please read the guidelines before posting]
– – – 
I am trying to make a sound when a ball hits the racket in his tutorial, but it won’t work.
#pragma strict

var ballSpeed : float = 40;

function Start () {
yield WaitForSeconds(2);
GoBall();
}

function onCollisionEnter2D (colInfo : Collision2D) {
if(colInfo.collider.tag == “Player”) {

GetComponent.().velocity.y = GetComponent.().velocity.y/2 + GetComponent.().velocity.y/3;
GetComponent.().pitch = Random.Range(0.8f,1.2f);
GetComponent.().Play();
}

}

function ResetBall(){
GetComponent.().velocity.y = 0;
GetComponent.().velocity.x = 0;
transform.position.x = 0;
transform.position.y = 0;

yield WaitForSeconds (0.5);
GoBall();
}

function GoBall (){
var randomNumber = Random.Range(0, 2);
if(randomNumber <= 0.5){
GetComponent.().AddForce (new Vector2(ballSpeed, 10));
}
else{
GetComponent.().AddForce(new Vector2(-ballSpeed, -10));
}
}

  • Liked by
Reply
3 on January 26, 2016

GetComponant. (). is this the right snytax?

Make the audiosource a variable the you can access and play it

Guru
on January 26, 2016

No it should be

GetComponent<Audiosource>().Play();
 For audiosources

on January 26, 2016

GetComponent.().Play() didn’t work either

on January 26, 2016

GetComponent..Play() didn’t work

Show more replies
  • Liked by
Reply
Cancel
5 on January 26, 2016

At first my answer was kind of long but then I slowly realised that you made quite a lot of errors with the GetComponent part. So here is what I suggest:
Watch this video: GetComponent-tutorial
That should teach you to use it properly.
Then try updating your code based on that new knowledge and let me know how it turns out 🙂

on January 26, 2016

Yes GetComponent.().Play() didn’t work. Could you just write out that code properly please?

Master
on January 26, 2016

No, watch the video and it will make sense to you why its currently not working. Why would you not want to learn how to use it properly? If I just write it for you, you will have some functioning code but it still wont make sense to you… people who think like that are the ones that post 5 questions a day. Learn HOW to make stuff before making stuff.

Wise
on January 26, 2016

Have to agree with . Watch the tutorials first. They are short and simple and will give you a better understanding of what and how things are happening. There are plenty of mistakes in your code.

Wise
on January 26, 2016

Yep, similarly. The best way to get better at making games is to learn and try and be much more indiependant with your programming and game making 🙂

Learn from our mistakes haha

Show more replies
  • Liked by
Reply
Cancel
0 on January 26, 2016

Ok I tried GetComponent.().Play() and it didn’t work

  • Liked by
Reply
Cancel
1 on January 26, 2016

GetComponent.().Play() and it didn’t work

Helpful
on January 27, 2016

Can’t you read properly…..? go read the previous replies

Show more replies
  • Liked by
Reply
Cancel
0 on January 27, 2016

If you haven’t fixed the error yet, I’d be happy to explain to you what is going wrong here. (I won’t type the script out for you though)

So the problem you are having, as stated by the people before me, is that you type “GetComponent.().Play()”, however the problem here is the dot between “GetComponent” and the parenthesis.

This is because GetComponent is actually a built-in function inside Unity (much like Destroy), and as you know, functions are called by function name + parenthesis, (i.e.: Destroy()).
Setting a dot between GetComponent and the parenthesis is like treating those parenthesis as a part of GetComponent, so like treating “GetComponent” as a class and calling a variable “()” inside this class.

The GetComponent function also has a parameter; the name of the component you’d like to call. Calling a component can be done in two ways within UnityScript: Using the parameter inside the parenthesis, or using the parameter outside the parenthesis, as Job insisted before.
Putting this into example it would give:

//This line of code:
GetComponent([ComponentName]);
//acts the same as this line of code:
GetComponent<[ComponentName]>();
//Note: the first way is obsolete in C#
//Note: Ofcourse, replace [ComponentName] with the name of your component

I hope this somewhat helped you (and any others having trouble understanding GetComponent) 🙂

  • Liked by
Reply
Cancel