Getting the error because velocity

Does anyone know what i did wrong?

Assets/Scripts/Ball.cs(42,14): error CS1061: Type UnityEngine.Component' does not contain a definition forvelocity’ and no extension method velocity' of typeUnityEngine.Component’ could be found. Are you missing an assembly reference?

void OnCollisionEnter2D (Collision2D collision){
		Vector2 tweak = new Vector2 (Random.Range(0f, 0.2f), Random.Range (0f,0.2f));


		if (hasStarted && collision.gameObject.tag != "breakable") {
			AudioSource audio = GetComponent<AudioSource> ();
			audio.Play ();
			rigidbody2D.velocity += tweak;
		}

i solved it already myself :slight_smile:

Hi @Andraz_Cuderman,

Just for the benefit of others who may encounter the same/similar issue, did you resolve it with something like the following;

this.GetComponent<Rigidbody2D>().velocity += tweak;

yes i reolved it with that code:

GetComponent< Rigidbody2D>().velocity += tweak;

1 Like

Thanks for sharing :slight_smile:

I wanted to alter my code to stop the “boing” sound from happening when my ball hit the bricks. To do that I altered my code to this.

if(hasStarted)
		{
		if(!collision.gameObject.GetComponent<Brick>())
		{
		 this.GetComponent<AudioSource>().Play(); 

Once I did this my code did not look like Ben’s at all. I tried putting the code he placed into all of the different code blocks but none of this helped my cause. I then tried the code you listed above

this.GetComponent<Rigidbody2D>().velocity += tweak;

The new line took care of my problems and I put it literally one line below the

this.GetComponent<AudioSource>().Play();  line. 

Tim

1 Like

I also just noticed that when you copy and paste from mono development onto this platform the <> brackets and their contents do not appear in your message.

1 Like

Hi Tim,

That would be because the forum supports various markup languages and tries to parse what you enter.

If you want to paste code into the forum it is worth applying a simple formatting to it.


See also;

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms