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 trialAdam Paciorek
3,181 PointsWhy when initiating targetRotation variable we did not specify it as private?
The previous variables like moveVertical or movement have been declared as private, how is targetRotation different?
1 Answer
Seth Kroger
56,413 PointsBecause targetRotation is declared inside a method or block (an enclosed set of curly braces) its scope, or where in can be accessed, would already restricted to that method or block. So all variables inside a method are considered local and don't need an access modifier like private.
public class PlayerMovement : MonoBehaviour {
private float turningSpeed = 20f; //...available to any method in the class
void FixedUpdate () {
if (movement != Vector3.zero) {
Quaternion targetRotation = Quaternion.LookRotation(movement, Vector3.up);
// ... can access targetRotation anywhere inside here
}
// but can't access it outside of the if block.
} // end method
} // end class
Adam Paciorek
3,181 PointsAdam Paciorek
3,181 PointsThank you Seth!