Choices to shape your future. Choose from a wide variety of graduate and professional programs, from optometry and health professions to education, writing and social work. Become a leader in healthcare with Pacific University's wide selection of health professions programs.
GetComponent() is a Unity method from the MonoBehaviour class that is used to access data on other components. To fully understand how this method works, you have to wrap your head around a few different concepts.
First, you declare a variable of type Camera to hold a reference to the Camera Component which you are going to find on the same GameObject. Next, you call GetComponent() in itâs generic version on the type Camera, which will return the first object of type Camera that is found. 1. mainCamera = GetComponent (Camera) as Camera; Above is the âstandardâ version of GetComponent() , which might be easier to understand, because it is slightly more explicit. The method searches for a class that is called Camera by string and then passes the class as the type of Camera so that we can store it in our variable. The as operator can be used to convert classes (they must be a reference type, so that they are able to return null !).