Need help! Error in script.

Assets/Scripts/BackgroundParallax.cs(19,30): error CS0117: Camera' does not contain a definition for main’

using UnityEngine;
using System.Collections;

public class BackgroundParallax : MonoBehaviour
{
	public Transform[] backgrounds;				// Array of all the backgrounds to be parallaxed.
	public float parallaxScale;					// The proportion of the camera's movement to move the backgrounds by.
	public float parallaxReductionFactor;		// How much less each successive layer should parallax.
	public float smoothing;						// How smooth the parallax effect should be.


	private Transform cam;						// Shorter reference to the main camera's transform.
	private Vector3 previousCamPos;				// The postion of the camera in the previous frame.


	void Awake ()
	{
		// Setting up the reference shortcut.
		cam = Camera.main.transform;
	}


	void Start ()
	{
		// The 'previous frame' had the current frame's camera position.
		previousCamPos = cam.position;
	}


	void Update ()
	{
		// The parallax is the opposite of the camera movement since the previous frame multiplied by the scale.
		float parallax = (previousCamPos.x - cam.position.x) * parallaxScale;

		// For each successive background...
		for(int i = 0; i < backgrounds.Length; i++)
		{
			// ... set a target x position which is their current position plus the parallax multiplied by the reduction.
			float backgroundTargetPosX = backgrounds_.position.x + parallax * (i * parallaxReductionFactor + 1);_
  •  	// Create a target position which is the background's current position but with it's target x position.*
    

Vector3 backgroundTargetPos = new Vector3(backgroundTargetPosX, backgrounds_.position.y, backgrounds*.position.z);*_

* // Lerp the background’s position between itself and it’s target position.*
backgrounds.position = Vector3.Lerp(backgrounds_.position, backgroundTargetPos, smoothing * Time.deltaTime);
* }*_

* // Set the previousCamPos to the camera’s position at the end of this frame.*
* previousCamPos = cam.position;*
* }*
}
Assets/Scripts/BackgroundParallax.cs(19,30): error CS0117: Camera' does not contain a definition for main’

Compiles fine on my machine. Can you copy the error message(s) from the console and add them to your question?

What is the error?

3 Answers

3

You are calling Camera.main – so you need a main camera. You can do this by selecting the camera you want as your primary camera, and changing the tag to MainCamera.

–edit–
Alternatively if you are creating your camera object by script, you can set the tag using this prior to calling Camera.main:

[cameraVariable].tag = “MainCamera”;

The camera gameObject is called MainCamera, and it's tagged MainCamera. So I don't know what the problem is.

not the gameObject, the Tag of the camera should be MainCamera. It's a built in tag.

I said it's tagged MainCamera if that's what your trying to mean.

I just deleted the script and everything works just fine.

For everyone’s reference, I ran into this error after importing Playmaker into one of my old Unity projects. The problem in my case was that one of the Playmaker scripts (CutToCamera.cs) has an instance variable of type Camera, and I also had a custom script class also called Camera, which caused a naming conflict. I copied the code from my Camera script to a new script called GameCamera, deleted my old Camera script, and assigned the new GameCamera script to my camera GameObject which fixed the error.