0
\$\begingroup\$

So, I have built a script in unity in c# called parralax. It basically parallaxes two backgrounds and looks like this:-

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

public class Parralax : MonoBehaviour {

    public float startPos;
    public GameObject cam;
    public float parallaxEffect;

    public float length;
    public GameObject player;
    public float playerStartPos;
    public float maxDisplacement;
    void Start () {
        maxDisplacement = 0f;
        playerStartPos = player.transform.position.x;
        startPos = transform.position.x;
        cam = GameObject.Find ("Main Camera");
        length = GetComponent <SpriteRenderer> ().bounds.size.x;
    }


    void Update () {

        float dist = cam.transform.position.x * parallaxEffect;
        transform.position = new Vector2 (startPos + dist, transform.position.y);

    }
}

If I put it in respective background and assign a value to it, it works fine and runs smoothly. But, if I play the game scene-wise i.e. 1st scene to 2nd scene to 3rd scene; it crashes and says:-

Unassigned reference exception:- You need to set variable cam in inspector

Even though, it runs on the same scene, it doesn't run after I play it scene- wise. Please help!

\$\endgroup\$
2
\$\begingroup\$

When you change scenes, the camera gets destroyed

Because the camera gets destroyed (and the new scene has a new one) your cam variable no longer points to the main camera. You can detect this and do the appropriate thing,

void Update () {
    if(cam == null) cam = Camera.main
    float dist = cam.transform.position.x * parallaxEffect;
    transform.position = new Vector2 (startPos + dist, transform.position.y);
}
\$\endgroup\$
  • \$\begingroup\$ Good catch. As he said the 'cached' cam will be 'null' for a split second so even if the new scene assigns the new one. The error will still register. Typically should always be checking a null in that case for good practice. \$\endgroup\$ – Justin Markwell Oct 29 at 17:03
  • \$\begingroup\$ Aborting-if-still-null would be a good idea here too, sure, though was't the main issue being addressed, which was that once it went null, it would stay null forever. (And not actually-null-because-Unity-wrapper-objects, but may as well be null). \$\endgroup\$ – Draco18s Oct 29 at 20:18
0
\$\begingroup\$

Like it says. "You need to set variable cam in inspector".

To do this:

  1. In Hierarchy panel right and click choose "Create Empty" to add a new Game Object and rename it as ParralaxController (you can give any name you want).
  2. In Inspector panel press Add Component and type Parralax then choose the Parralax script below.
  3. When this ParralaxController is selected drag your "Main Camera" and drop it to the area of Cam in Inspector Panel.

Alternatively define your camera object like this:

public Camera cam;

and in Start Method write this.

cam = Camera.main;
\$\endgroup\$
  • 1
    \$\begingroup\$ cam = Camera.main is (effectively) the same as GameObject.Find ("Main Camera");. Camera.main is just a property-method that calls GameObject.FindWithTag, so changing scenes is going to leave cam just as undefined when the main camera gets destroyed. \$\endgroup\$ – Draco18s Oct 29 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.