Parrelaxing issue in Brackeys tutorial

Updated on October 25, 2014 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on October 25, 2014

I was taking the “Bullet Trail” (https://www.youtube.com/watch?v=1KdS0QzyhCg&list=PLPV2KyIb3jR42oVBU6K2DIL6Y22Ry9J1c) and I messed up the code somehow, so I took a break from it for a week and went back to it. I deleted the changes I had made to the weapon script, and tried to play the game, and while it worked the mountains in the background wirred all over the place extremely fast. I got the error:
NullReferenceException: Object reference not set to an instance of an object
Parallaxing.Update () (at Assets/Parallaxing.cs:40)

I had never seen a problem with the parralaxing before, and apparently it had something to do with both that script and the camerafollow2d script. Can anyone help? Here the the scripts:

Parralaxing:

using UnityEngine;
 using System.Collections;
 public class Parallaxing : MonoBehaviour {
 public Transform[] backgrounds; // Array (list) of all the back- and foregrounds to be parallaxed
 private float[] parallaxScales; // The proportion of the camera's movement to move the backgrounds by
 public float smoothing = 1f; // How smooth the parallax is going to be. Make sure to set this above 0
 private Transform cam; // reference to the main cameras transform
 private Vector3 previousCamPos; // the position of the camera in the previous frame
 // Is called before Start(). Great for references.
 void Awake () {
 // set up camera the reference
 cam = Camera.main.transform;
 }
 // Use this for initialization
 void Start () {
 // The previous frame had the current frame's camera position
 previousCamPos = cam.position;
 // asigning coresponding parallaxScales
 parallaxScales = new float[backgrounds.Length];
 for (int i = 0; i < backgrounds.Length; i++) {
 parallaxScales[i] = backgrounds[i].position.z*-1;
 }
 }
 // Update is called once per frame
 void Update () {
 // for each background
 for (int i = 0; i < backgrounds.Length; i++) {
 // the parallax is the opposite of the camera movement because the previous frame multiplied by the scale
 float parallax = (previousCamPos.x - cam.position.x) * parallaxScales[i];
 // set a target x position which is the current position plus the parallax
 float backgroundTargetPosX = backgrounds[i].position.x + parallax;
 // create a target position which is the background's current position with it's target x position
 Vector3 backgroundTargetPos = new Vector3 (backgroundTargetPosX, backgrounds[i].position.y, backgrounds[i].position.z);
 // fade between current position and the target position using lerp
 backgrounds[i].position = Vector3.Lerp (backgrounds[i].position, backgroundTargetPos, smoothing * Time.deltaTime);
 }
 // set the previousCamPos to the camera's position at the end of the frame
 previousCamPos = cam.position;
 }
 }

And the camerafollow2d script:

 using UnityEngine;
 using System.Collections;
 public class Camera2DFollow : MonoBehaviour {
 public Transform target;
 public float damping = 1;
 public float lookAheadFactor = 3;
 public float lookAheadReturnSpeed = 0.5f;
 public float lookAheadMoveThreshold = 0.1f;
 float offsetZ;
 Vector3 lastTargetPosition;
 Vector3 currentVelocity;
 Vector3 lookAheadPos;
 // Use this for initialization
 void Start () {
 lastTargetPosition = target.position;
 offsetZ = (transform.position - target.position).z;
 transform.parent = null;
 }
 // Update is called once per frame
 void Update () {
 // only update lookahead pos if accelerating or changed direction
 float xMoveDelta = (target.position - lastTargetPosition).x;
 bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;
 if (updateLookAheadTarget) {
 lookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta);
 } else {
 lookAheadPos = Vector3.MoveTowards(lookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed); 
 }
 Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
 Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
 transform.position = newPos;
 lastTargetPosition = target.position; 
 }
 }

  • Liked by
Reply