Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in '2D' started by Matteo19, Jul 8, 2019.

  1. Matteo19

    Matteo19

    Joined:
    Dec 21, 2018
    Posts:
    1
    I can't figure out what is the problem...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent (typeof(SpriteRenderer))]
    6.  
    7. public class tiling : MonoBehaviour
    8. {
    9.     public int offsetX = 2;
    10.     public bool hasARightBuddy = false;
    11.     public bool hasALeftBuddy = false;
    12.     public bool reverseScale = false;
    13.  
    14.     private float spriteWidth = 0f;
    15.     private Camera cam;
    16.     private Transform myTransform;
    17.  
    18.     void Awake()
    19.     {
    20.         cam = Camera.main;
    21.         myTransform = myTransform;
    22.     }
    23.  
    24.     void Start()
    25.     {
    26.         SpriteRenderer sRenderer = GetComponent<SpriteRenderer>();
    27.         spriteWidth = sRenderer.sprite.bounds.size.x;
    28.     }
    29.  
    30.     void Update()
    31.     {
    32.         if (hasALeftBuddy == false || hasARightBuddy == false)
    33.         {
    34.             // calculate the cameras extend of what it can see in world coords
    35.             float camHorizontalExtend = cam.orthographicSize * Screen.width / Screen.height;
    36.  
    37.             // calculate x pos where the camera can see edge of sprite
    38.             float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth / 2) - camHorizontalExtend;
    39.             float edgeVisiblePositionLeft = (myTransform.position.x - spriteWidth / 2) + camHorizontalExtend;
    40.  
    41.             // check if we can see the edge of the element
    42.             if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)
    43.             {
    44.                 MakeNewBuddy(1);
    45.                 hasARightBuddy = true;
    46.             }
    47.             else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && hasARightBuddy == false)
    48.             {
    49.                 MakeNewBuddy(-1);
    50.                 hasALeftBuddy = true;
    51.             }
    52.         }
    53.     }
    54.  
    55.     // creates a buddy on the required side
    56.     void MakeNewBuddy(int rightOrLeft)
    57.     {
    58.         // calculate new pos of the new buddy
    59.         Vector3 newPosition = new Vector3(myTransform.position.x + spriteWidth * rightOrLeft, myTransform.position.y, myTransform.position.z);
    60.        
    61.         // instantiating new buddy and storing him in a variable
    62.         Transform newBuddy = Instantiate(myTransform, newPosition, myTransform.rotation) as Transform;
    63.  
    64.         if (reverseScale == true)
    65.         {
    66.             newBuddy.localScale = new Vector3(newBuddy.localScale.x*-1, newBuddy.localScale.y, newBuddy.localScale.z);
    67.         }
    68.  
    69.         newBuddy.parent = myTransform.parent;
    70.         if (rightOrLeft>0)
    71.         {
    72.             newBuddy.GetComponent<tiling>().hasALeftBuddy = true;
    73.         }
    74.         else
    75.         {
    76.             newBuddy.GetComponent<tiling>().hasARightBuddy = true;
    77.         }
    78.     }
    79. }
    80.  
    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:

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (hasALeftBuddy == false || hasARightBuddy == false)
    4.         {
    5.             // calculate the cameras extend of what it can see in world coords
    6.             float camHorizontalExtend = cam.orthographicSize * Screen.width / Screen.height;
    7.  
    8.            // calculate x pos where the camera can see the edge of sprite
    9.  *HERE*  float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth / 2) - camHorizontalExtend;
    10.             float edgeVisiblePositionLeft = (myTransform.position.x - spriteWidth / 2) + camHorizontalExtend;
    11.  
    12.             // check if we can see the edge of the element
    13.             if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)
    14.             {
    15.                 MakeNewBuddy(1);
    16.                 hasARightBuddy = true;
    17.             }
    18.             else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && hasARightBuddy == false)
    19.             {
    20.                 MakeNewBuddy(-1);
    21.                 hasALeftBuddy = true;
    22.             }
    23.         }
    24.     }
     

    Attached Files:

  2. amitbu

    amitbu

    Joined:
    May 16, 2019
    Posts:
    12
    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.
     
  3. TigerDawn

    TigerDawn

    Joined:
    Aug 4, 2018
    Posts:
    21
    setting null to null means its null.

    myTransform = myTransform;