As you can see in the attached image below and the code, I have added a class that implements the interface ICameraControl attached to a GameObject.

When I call var controller = GetComponent<ICameraControl>();, I get nothing returned (null), as in this example:

public class KeyboardHandler : MonoBehaviour
    private void Start()
        var controller = GetComponent<ICameraControl>();
        if (null == controller)
            print("no ICameraControl found!");

And the output is like this in the unity console:

no ICameraControl found! UnityEngine.MonoBehaviour:print(Object) Assets.Player.KeyboardHandler:Start() (at Assets/Player/KeyboardHandler.cs:18)

Here's the Camera Position Handler attached to my game object: enter image description here

Here's the interface implementation:

public class CameraPositionHandler : MonoBehaviour, ICameraControl
    private GameObject mainCamera;
    private Quaternion homePosition;

    private void Start()
        mainCamera = GameObject.FindGameObjectWithTag("MainCamera");
        homePosition = mainCamera.transform.rotation;

    private void LateUpdate() { } 

Any ideas what I am doing wrong?

Much appreciated :) Matt

Edit: The keyboard script is on my player object (at bottom of the picture of the player object). And the Camera Position Handler is on the Camera Arm object. The Player object and Camera Arm objects are at the same level.

enter image description hereenter image description here

  • \$\begingroup\$ Your KeyboardHandler script is not visible on the object with your ICameraControl script. Can you show us what object your KeyboardHandler is on, and its relationship to your ICameraControl within the scene hierarchy? \$\endgroup\$ – DMGregory Feb 16 '19 at 19:17
  • \$\begingroup\$ @DMGregory yes. See update. I hope that answers your questions. \$\endgroup\$ – tatmanblue Feb 16 '19 at 19:40

Consulting the docs on GameObject.GetComponent:

Returns the component of Type type if the game object has one attached, null if it doesn't.

So when your KeyboardHandler script says GetComponent<ICameraControl>(), that's equivalent to:


ie. "Give me an instance of component attached to the same GameObject as I am, that implements the ICameraControl interface"

But you don't have an ICameraControl component on the KeyboardHandler's game object, so the method correctly returns null.

It looks like what you might have meant was either:


which would look for an ICameraControl on some other object you've provided, either via an Inspector reference or by looking it up with eg. Camera.main.

Or, the nuclear option:


which searches your whole scene for any instance of a matching type, attached to any object.

  • \$\begingroup\$ Yeah. I tried 'FindObjectOfType<ICameraControl>()' before asking the question. I get the following compile error: The type 'Assets.Scripts.ICameraControl' cannot be used as type parameter 'T' in the generic type or method 'Object.FindObjectOfType<T>()'. I admit I didn't spend a ton of time researching the error. I moved on to trying other things. I wanted to keep hard coded references to implementation types out of my code, if possible. maybe that is not possible and have to get the object by name. \$\endgroup\$ – tatmanblue Feb 16 '19 at 20:41
  • \$\begingroup\$ Is the better option to move CameraPositionHandler into the player object? \$\endgroup\$ – tatmanblue Feb 16 '19 at 20:45
  • \$\begingroup\$ Try using the non-generic form, FindObjectOfType(typeof(ICameraControl)). Or there are other decoupling patterns you can use, depending on the relationships between your objects. \$\endgroup\$ – DMGregory Feb 16 '19 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.