Simply calling a method from another script :(

I’m new to Unity (via AS3), and have scoured the forum trying different things, but I am proper stuck. Must be a n00bie hurdle :frowning:

All I want to do is control a camera rotation with a GUI button and, at present, my error is “Unexpected token: cam”. I would really love some help :slight_smile:

My ButtonScript.js is…

var customSkin:GUISkin;

var cam: CameraScript;

function Start () {

}

function Update () {

}

function OnGUI(){
if(GUI.Button( Rect(10, 10, 320, 80), “Cha…”))
{
cam: GameObject.FindWithTag(“MainCamera”).GetComponent(CameraScript);
cam.moveCameraPlus();
}
}

and my CameraScript.js

var target:Transform;

function Start () {
transform.LookAt(target);
}

function Update () {
moveHorz = Input.GetAxis(“Horizontal”);
if (moveHorz > 0){
moveCameraPlus();
}
else if (moveHorz < 0){
moveCameraNeg();
}
}

public function moveCameraPlus () {
transform.LookAt(target);
transform.Translate(Vector3(0,100,0).right * (Time.deltaTime));
}

public function moveCameraNeg () {
transform.LookAt(target);
transform.Translate(Vector3(0,100,0).left * (Time.deltaTime));
}

There’s an error: you’re not assigning correctly the CameraScript to cam:

function OnGUI(){ 
  if(GUI.Button( Rect(10, 10, 320, 80), "Cha...")) { 
    cam = GameObject.FindWithTag("MainCamera").GetComponent(CameraScript);
    cam.moveCameraPlus(); 
  } 
}

But there’s a property available to access the main camera directly:

function OnGUI(){ 
  if(GUI.Button( Rect(10, 10, 320, 80), "Cha...")) { 
    cam = Camera.main.GetComponent(CameraScript);
    cam.moveCameraPlus(); 
  } 
}

Or - better yet - you could cache the camera script in Start:

function Start(){
  cam = Camera.main.GetComponent(CameraScript);
}

function OnGUI(){ 
  if(GUI.Button( Rect(10, 10, 320, 80), "Cha...")) { 
    cam.moveCameraPlus(); // just use the cached camera
  } 
}