Why is there no camera attached, even if I've attached it?

So, i want to use camera.ViewportToWorldPoint() to show the bottom center bounds of my screen. So, I created a script, add that component to my object that need it.

using UnityEngine;
using System.Collections;

public class PathMovement : MonoBehaviour {

	public Camera cam;
	private Vector3 bound;

	void Awake () {
		cam = GetComponent<Camera> ();
	}

	void Start(){
		bound = cam.ViewportToWorldPoint (new Vector3 (0f, 0.5f));
		Debug.Log (bound);
	}
}

and then, I attach the MainCamera via the GUI

53481-cam.jpg

And then, when I run it, there’s still an error says :

MissingComponentException: There is no ‘Camera’ attached to the “RiverPath” game object, but a >script is trying to access it.
You probably need to add a Camera to the game object “RiverPath”. Or your script needs to check if >the component is attached before using it.
UnityEngine.Camera.ViewportToWorldPoint (Vector3 position) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineCamera.gen.cs:408)
PathMovement.Start () (at Assets/Scripts/PathMovement.cs:21)

This is quite weird since I’ve attached the main camera, but somehow unity didn’t detect that. I also have tried to put the cam = GetComponent<Camera>(); on Awake() as well as Start(), but none work. :frowning:

Btw, I am making a mobile application on android. And also using unity 5.

Is there any way to do it properly??
Thanks.

Remove GetComponent (); in awake and everything should be fine.

You are are mistaking couple things. You’ve already linked the reference of your camera to variable cam via inspector. So getting the component is not needed. You can clearly see that this gameobject does not contain camera component in inspector. What happens is that in awake you are overwriting the cam reference with whatever is found from getcomponent, but it returns null as you don’t have the camera on that gameobject.