1
\$\begingroup\$

I want to use camera.ViewportToWorldPoint() to show the bottom center bounds of my screen. So, I created a script and added that component to my object that needs 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

enter image description here

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. :(

This is an Android app, using Unity 5.

How do I do this correctly?

\$\endgroup\$
3
\$\begingroup\$

i think cam = GetComponent<Camera> ();

is useless and wrong as you already assigned cam in the ispector.

\$\endgroup\$
  • \$\begingroup\$ It would work fine if you attached that script to a GameObject with a Camera component attached... But you didn't... Hence the MissingComponentException. \$\endgroup\$ – Savlon Sep 5 '15 at 13:03

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.