Click Obect to fire a camera method

Hi guys, im very new to unity. I know this question has been asked a number of times but i cant seem to get any solutions working…

Currently i have a few cubes. When i click one of these cube game objects i would like to attach the main camera to it.

Each of my game objects has the following function… The function seems to fire ok when i debug it, the problem however is that it can not find the MoveToObject method.

void OnMouseDown() {
Camera.main.MoveToObject();
}

Now my main camera has a script attached to it called RtsCamera. The RtsCamera has a
method named MoveToObject.

using UnityEngine; using
System.Collections;

public class RtsCamera : MonoBehaviour
{

// Use this for initialization void
Start () { } // Update is called
once per frame void Update () { }
public void MoveToObject() {
Debug.Log(“Yay getting somewhere”);
} }

Error

Assets/clicky.cs(17,29): error CS1061: Type UnityEngine.Camera' does not contain a definition for MoveToObject’ and no extension method MoveToObject' of type UnityEngine.Camera’ could be found (are you missing a using directive or an assembly reference?)

Thanks in advance,

What you want is :

1:when you click on an cube you want to attach the camera to that cube. solution: what you need to do is parent the camera to that object.

2:in the script you have posted, you can not access MoveToObject() function. solution: you need to create an RtsCamera script object, assign the component to it then access the function.

Solution1:

 private Transform cameraTransform = Camera.main.transform;    
void OnMouseDown() 
{
     cameraTransform.parent = transform; // makes the camera child of the object this script is attached to.

    }

Solution 2: if you want to access MoveToObject() function in another script then,

private RtsCamera m_cameraScript;

void Start() 
{

    m_cameraScript = Camera.main.Transform.GetComponent("RtsCamera");

    }

void OnMouseDown()
{
    m_cameraScript.MoveToObject();
}