- Home /
Confused about one part of inheriting function from another objects script
OK I get that this works, I use it...
void Death () {
GameObject paddleObject = GameObject.Find("paddle");
PaddleScript paddleScript = paddleObject.GetComponent<PaddleScript>();
paddleScript.LoseLife();
}
But what I dont get is the first part of the second line, "PaddleScript paddleScript ="
What the hell is PaddleScript? I never call it. I only use paddleScript. What is the first part? I'm so confused by this.
Answer by whydoidoit · Feb 15, 2014 at 07:16 AM
You are declaring a variable called paddleScript, it's type is PaddleScript which is the name of a file (and class) in your project.
It's just like int - means integer:
int i = 5;
The PaddleScript paddleScript = line is getting a reference to an instance of that class which is attached to the object called "paddle"
In Unity this isn't "inheritance" it's "composition" so your "paddle" object will have a bunch of classes attached to it and the way you get access to them is GetComponent.
Answer by Mikejr76 · Feb 15, 2014 at 07:21 AM
PaddleScript is a component on the paddleObject. If you have a gameobject called paddleobject selected and if you were to go to the inspector and add a component, and that component was a script and named it PaddleScript. Then you went to the top of the inspector and tagged that object with the tag "paddle". Then the first line would get you a reference to the paddleobject. The second line would get you a reference to the PaddleScript component on that paddle object.
PaddleScript is a class or a datatype ( a blue print for an object). Which when instantiated is an object.