Trying to screen wrap x only

Why am i getting a reference error when trying to screen wrap my player to x

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlaneController : MonoBehaviour
{
    Rigidbody2D rb;
    [Tooltip("World units per second.")]
    public float moveSpeed;
    [Tooltip("Degrees per second.")]
    public float rotateAmount;
    float rot;
   
    private Renderer[] renderers;
    private bool isWrappingX = false;

    SpriteRenderer spriteRenderer;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Vector3 mousePos = Input.mousePosition;
            mousePos.z = 1;    // gotta cast a little bit into the scene!

            mousePos = Camera.main.ScreenToWorldPoint(mousePos);

            if (mousePos.x < 0)
            {
                rot = rotateAmount;
            }
            else
            {
                rot = -rotateAmount;
            }

            transform.Rotate(0, 0, rot * Time.deltaTime);
        }

        else
        {
            SetFlipY();
        }
    }

    private void FixedUpdate()
    {
        rb.velocity = transform.right * moveSpeed;
        ScreenWrap();
    }

    void SetFlipY()
    {
        // I'm not going to base it off of rot but rather off of the
        // sign of the x component of the transform.right vector.
        bool flipy = transform.right.x < 0;
        spriteRenderer.flipY = flipy;
    }

   void ScreenWrap()
    {
        bool isVisible = CheckRenderers();

        if (isVisible)
        {
            isWrappingX = false;
            return;
        }

        if (isWrappingX)
        {
            return;
        }

        Vector3 newPosition = transform.position;

        if (newPosition.x > 1 || newPosition.x < 0)
        {
            newPosition.x = -newPosition.x;
            isWrappingX = true;
        }

        transform.position = newPosition;
    }

    bool CheckRenderers()
    {
        foreach (Renderer renderer in renderers)
        {
            if (renderer.isVisible)
            {
                return true;
            }
        }

        return false;
    }
}

Because something is null!

If you posted which line was failing, it might even be able to figure out what was null.

when i click on the error it takes me here

foreach (Renderer renderer in renderers)

Well, you never assign anything to renderers, so that makes sense.

i followed a tutorial on youtube, any suggestions on how i can fix this?
thanks for your input, appreciate it

If it works in the tutorial, but not for you, you didn’t follow the tutorial exactly!

renderers is probably either supposed to be public (and filled from the inspector), or you’re supposed to assign something to it in Start.

1 Like

ok will check again, thanks Baste!!!

Yes!!! there was a line missing in the start function, working great!!! Thanks again Baste!!!