NullReferenceException: Object reference not set to an instance of an object

I can’t figure out what is the problem…

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

[RequireComponent (typeof(SpriteRenderer))]

public class tiling : MonoBehaviour
{
    public int offsetX = 2;
    public bool hasARightBuddy = false;
    public bool hasALeftBuddy = false;
    public bool reverseScale = false;

    private float spriteWidth = 0f;
    private Camera cam;
    private Transform myTransform;

    void Awake()
    {
        cam = Camera.main;
        myTransform = myTransform;
    }

    void Start()
    {
        SpriteRenderer sRenderer = GetComponent<SpriteRenderer>();
        spriteWidth = sRenderer.sprite.bounds.size.x;
    }

    void Update()
    {
        if (hasALeftBuddy == false || hasARightBuddy == false)
        {
            // calculate the cameras extend of what it can see in world coords
            float camHorizontalExtend = cam.orthographicSize * Screen.width / Screen.height;

            // calculate x pos where the camera can see edge of sprite
            float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth / 2) - camHorizontalExtend;
            float edgeVisiblePositionLeft = (myTransform.position.x - spriteWidth / 2) + camHorizontalExtend;

            // check if we can see the edge of the element
            if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)
            {
                MakeNewBuddy(1);
                hasARightBuddy = true;
            }
            else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && hasARightBuddy == false)
            {
                MakeNewBuddy(-1);
                hasALeftBuddy = true;
            }
        }
    }

    // creates a buddy on the required side
    void MakeNewBuddy(int rightOrLeft) 
    {
        // calculate new pos of the new buddy
        Vector3 newPosition = new Vector3(myTransform.position.x + spriteWidth * rightOrLeft, myTransform.position.y, myTransform.position.z);
       
        // instantiating new buddy and storing him in a variable
        Transform newBuddy = Instantiate(myTransform, newPosition, myTransform.rotation) as Transform;

        if (reverseScale == true)
        {
            newBuddy.localScale = new Vector3(newBuddy.localScale.x*-1, newBuddy.localScale.y, newBuddy.localScale.z);
        }

        newBuddy.parent = myTransform.parent;
        if (rightOrLeft>0)
        {
            newBuddy.GetComponent<tiling>().hasALeftBuddy = true;
        }
        else
        {
            newBuddy.GetComponent<tiling>().hasARightBuddy = true;
        }
    }
}

The code doesn’t work but it runs, this should create a new foreground when the camera sees the edge of the foreground.

Then it says this:
tiling.Update () (at Assets/tiling.cs:38) which is this line:

void Update()
    {
        if (hasALeftBuddy == false || hasARightBuddy == false)
        {
            // calculate the cameras extend of what it can see in world coords
            float camHorizontalExtend = cam.orthographicSize * Screen.width / Screen.height;

           // calculate x pos where the camera can see the edge of sprite
 *HERE*  float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth / 2) - camHorizontalExtend;
            float edgeVisiblePositionLeft = (myTransform.position.x - spriteWidth / 2) + camHorizontalExtend;

            // check if we can see the edge of the element
            if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)
            {
                MakeNewBuddy(1);
                hasARightBuddy = true;
            }
            else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && hasARightBuddy == false)
            {
                MakeNewBuddy(-1);
                hasALeftBuddy = true;
            }
        }
    }

4724435–447167–tiling.cs (2.62 KB)

Forgive me If i’m wrong but you never assign a value to myTransform, You just declare it but never assign a value to it.

setting null to null means its null.

myTransform = myTransform;