[Solved] Why using rigidbody2d.velocity and not this.rigidbody2d.velocity?

This is not very clear to me. We used the keyword this to reference the instance throughout all of the code. Then, in this lesson, we get rid of it when we use rigidbody2D.velocity inside the OnCollisionEnter2D event.
Can someone explain me the distinguishing between the different syntax we’re using here?
Thanks a lot.

Hello,

“this” refers to the script itself, while gameObject is the Object. But you don’t have to explicity tell the script that it is about the gameObject, since it is somehow the default form.

rigibody2d.velocity is the same as gameObject.rigibody2d, (the rigidbody is a component of the gameObject). this.rigidbody won’t work because the rigidbody is attached to the gameObject and not to the script

Thanks a lot Joao! I learned we used this to reference the script and not the game object.
For me the problem is understanding another thing… During the course, in the Start and the Update methods, the teachers made us use the keyword “this” for handling both transform.position and rigidbody2d.velocity of the ball.
Then, only in the OnCollisionEnter2D, the “this” keyword is gone… I understand “this” is used to reference Ball.cs and not the GameObject, but weren’t we referencing the GameObject all the same during the Start and Update methods?

Please help me understand this I’m feeling so stupid right now :confounded:
Here is the script:

using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
private Paddle paddle;
private bool hasStarted = false;
private Vector3 paddleToBallVector;

// Use this for initialization
void Start () {
paddle = GameObject.FindObjectOfType();
paddleToBallVector = this.transform.position - paddle.transform.position;
}

// Update is called once per frame
void Update () {
if (!hasStarted) {
this.transform.position = paddle.transform.position + paddleToBallVector;

  	if (Input.GetMouseButtonDown(0)) {
  		print ("LMB Click, launch ball!"); 
  		**this**.rigidbody2D.velocity = new Vector2 (2f, 10f);
  		hasStarted = true;
  	}
  }		 

}

void OnCollisionEnter2D(Collision2D collision){
Vector2 tweak = new Vector2 (Random.Range (0f, 0.2f), Random.Range (0f, 0.2f));
rigidbody2D.velocity += tweak;
if (hasStarted) {
audio.Play ();
}
}

Space,

Sorry for misunderstanding the question,
I don’t know what make it don’t work under OnCollisionEnter2D, but ‘this’ is redundant since you can call the methods without the need to use it, this.gameObject.transform.position == transform.position. Perhaps the method OnCollisionEnter2D just don’t accept the input of “this”, but it is hard to tell why since we don’t have the access to the code within OnCollisionEnter2d, sorry for not being able to help :disappointed:

My fault not attaching any script to the questiond for clarity :smiley:
Hope somebody will answer me… I’m feeling like I can’t go on with the course without understanding this first lol

Hi,
Joao is correct in that the keyword ‘this’ refers to the script attached to the gameObject and not the gameObject itself.

Unity seems to allow for some wiggle room here so you can use either keyword in some instances and everything works.

Examples:
in the Start() method we used: this.rigidbody2D.velocity
and that worked. You could also use gameObject.rigidbody2d.velocity
Finally, in OnCollisionEnter2D we use neither and just say: rigidbody2D.velocity

So all three actually work thanks to Unity being smart but i think it can lead to bad practices and mistakes like if you try to Destroy() the gameObject but use the ‘this’ keyword and just destroy the script.

It wasn’t explicit if the instructor made a mistake or was just doing things differently to show they can be done.

Thank you! I understood the syntax and the logic behind it after a few more lessons :slight_smile: . Everything is completely clear now

Excellent! Sorry I was a bit late to the conversation but glad it’s all
cleared up.

Privacy & Terms