Unable to grab a reference to a Pixel Perfect Camera component

I’m wondering if this has to do with the way I am using these components…

First off, I have two of these Pixel Perfect Camera components attached to two different cameras. The reason I have two of them is because I needed a way to keep my UI pixel perfect, but also keep my game play pixel perfect.

I have a main camera and a UI camera. The UI camera only renders the UI, while the main camera renders everything else. The problem is, if I don’t have a pixel perfect component on the UI camera, I can’t keep the UI elements the same exact size proportions, depending on how I scale the main game play window. Therefore, I chose to use a Pixel Perfect Camera component, and set it to “WindowBox”. This keeps all my UI absolutely perfectly aligned and scaled just the way it should be when I scale my main game play window.

The issue is, this UI camera is an overlay to the main camera. When you stack one camera on top of the other, the Pixel Perfect Camera component displays a warning that it will not function correctly. So far, though, I haven’t run into any issues, except for trying to grab a reference to this component through code.

Here’s the component enabled -

8981782--1235770--Screenshot 2023-04-29 132142.png

Now, I grab a reference with this -

    private void Awake()
    {
        _mainCamera = GameObject.FindGameObjectWithTag("MainCamera");
    }

    private void Start()
    {
        _pixelPerfectCamera = _mainCamera.GetComponent<PixelPerfectCamera>();
    }

I’m wondering if it has something to do with the fact that the Pixel Perfect Camera component is giving me this warning that it doesn’t function correctly, or if I am doing something wrong here.

EDIT: For clarity, These two are in different functions (Start and Awake) simple as an experiment to see if it was a timing issue, but that’s not the case. _pixelPerfectCamera is still null regardless. I can’t make any sense of this, since the component is actually attached to the main camera game object, and is also enabled.

1 Like

Issue resolved.

For those of you that Google this same problem, I am on 2D URP. I was using this -

using UnityEngine.U2D;

When I should have been using this -

using UnityEngine.Experimental.Rendering.Universal;

I’m not exactly sure why the first one would not allow me to grab a reference, but there may be something about this in some documentation I haven’t bothered to read.

3 Likes

I’m thinking there has to be two different classes named PixelPerfectCamera, one in each of the two namespaces you tried. The actual object you attached was one of them and you had been asking for the other, so the GetComponent() said “Nope, none of those on here!”

Let’s go see!

Here’s one:

https://docs.unity3d.com/Packages/com.unity.2d.pixel-perfect@1.0/api/UnityEngine.U2D.PixelPerfectCamera.html

And here’s the other:

https://docs.unity3d.com/Packages/com.unity.render-pipelines.universal@7.1/api/UnityEngine.Experimental.Rendering.Universal.PixelPerfectCamera.html

One is just the old version of the class, one is the newer one. You probably would only want ONE of those two, whatever is the current shipping version for your context.