Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development How to Make a Video Game Player Input and Cameras Create a Target Rotation

Initializing Variable

On this video we initialized the variable "turningSpeed" like this:

private float turningSpeed = 20f;

and "playerRigidbody" like this:

playerRigidbody = GetComponent<playerRigidbody> ();

Couldn't we have done "playerRigidbody" the same as "turningSpeed"?

2 Answers

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Tony

the variable had to be defined this way because of the data type being stored inside the variable namely while the turning speed is simply asking for a floating point number (which we could assign any number we wished) the playerRigidBody variable was defined as a RigidBody type object which was the actual object (the frog) which the script was linked with.

The rigidBody is part of the frog so could use the method getComponent() to get the object from the frog (if that makes sense) you could have assigned it a new object of type rigidBody but then this wouldn't have referred to the same one the frog was using and therefore none of the code you wrote would have applied to the frog.

It sometimes get's confusing when passing in objects (classes) as variable types and parameters in methods, the big bonus is it allows you to group a lot of code/methods/variable together and allows them to passed along together

I hope this makes sense I feel like I'm losing myself lol

Daniel

I don't understand. In -> "Private RigidBody PlayerRigidBody" you said this is RigidBody body type object...is this some kind of a generalization? and it doesn't actually point to the Rigidbody component in the Inspector?
Now the second paragraph. is this a different Rigidbody? Which part of the frog is this exactly? What object? From what I am able to gather is that the first Rigidbody is a class but not exactly sure if it is related to the Rigidbody in Inspector. The second <RigidBody> is connected to the Inspector Rigidbody? I hope we're on the same page here. tks

I'm a little lost but kinda makes sense lol

Daniel Hartin
Daniel Hartin
18,106 Points

Okay Paul, this isn't my strong suit but I'll try to explain to the best of my knowledge.

The frog as you see it is actually a collection of various objects (Instances of classes) each object has it's own values and methods. When we attach a script to the frog however the script can't access any values outside the scope of it's class unless you create 'for lack of a better word links'.

Okay the RigidBody object that is attached to the frog has values like it's mass and velocity etc. etc. but the script knows none of these so we need to define the RigidBody 'link'.

The script could be attached to any GameObject we have, all with different values, so what we need to do it let the script know what these values are, we can do this by passing the RigidBody object of the GameObject (in this case the frog) through to the script by using the method getComponent() which returns the RigidBody of the object the script is attached to.

Now the script knows what these values are so when we tell the RigidBody to increase it's speed by 1 etc etc the script is now correctly changing the values of the RigidBody object which controls the physics of the frog.

Hopefully this makes more sense

Daniel

Dandiel, Now it totally makes sense. thanks for explaining. One more thing left to clarify on this. The objects of the GameObject, are all of them always listed in the inspector as in this case Transform, Animator, Rigitbody, and Box Collider ? Are all of them considered objects of the frog? How about all the components that make up the frog(player) inside the Hierarchy if you were to expand the drop downs, are they not considered objects ?

tks.