- Home /
How do i reference self to be a GameObject, not a Object? (or what i didn't understand right)
Here's my problem:
Script in non-Rigidbody2D object:
protected Rigidbody2D current;
public void MyFunction (bool check, GameObject obj) {
current = obj.GetComponent<Rigidbody2D>(); //error CS1061 here
if (check) {
current.position = Vector2;
} else {
current.position = Vector2.zero;
}
}
Script on Rigidbody2D object:
public Non-Rigidbody2DScript script;
void FixedUpdate(){
script .MyFunction(true, this);
}
As i saw using Debug.Log, the this in the Object referencing a Object type, not a GameObject... then the question: How i reference in function call in the Rb2D object itself with the GameObject reference? (or what i'm doing wrong in all this)
Answer by Legend_Bacon · Aug 17, 2018 at 11:59 AM
Hello there,
"this" actually refers to the script (or instance, rather), NOT the GameObject.
If you wanted to pass the GameObject in your MyFunction() call, then you would do this instead:
script .MyFunction(true, gameObject);
Hope that helps!
Cheers,
~LegendBacon
didn't know about gameObject
to reference self (ins$$anonymous$$d of self
script). Thankyou for your answer :D
Your answer

Follow this Question
Related Questions
Can't Return the Tag of a Child Object 4 Answers
Removing objects from an array 2 Answers
How to remove objects from a list ? 3 Answers
object not spawning but no error message 2 Answers