[JS] Get target from another script?

Hello, i have two scripts. One called “MouseOrbit” which is attached to my camera, and another script called “EnterVehicleScript” which is attached to my player.

Now what i want to do is:

//MouseOrbit Script

//Get this information
var target : Transform;
//EnterVehicleScript

//And use it in this script so i can change the target of "MouseOrbit" from within this script
var _enableVehicleEnter = false;

var isControllingPlayer = true;
var isControllingBoatVehicle = false;

//Transforms
var cam : Transform;
var player : Transform;
var boatVehicle : Transform;

function Start () 
{
	cam.GetComponent("MouseOrbit");
	
	//Make sure we are starting with controlling the player
	isControllingPlayer = true;
}

function Update () 
{
	//Enters the boat if button is pushed and if we are inside the collider
	if (_enableVehicleEnter == true  Input.GetButton("EnterVehicle"))
	{
		isControllingBoatVehicle = true;
	}

	//Make sure vehicleController and playerController can't be active at the same time
	
	
	//If we are controlling the player

	
	//If we are controlling the boat
	if (isControllingBoatVehicle == true)
	{
		MouseOrbit.target = boatVehicle;
	}
}

function OnTriggerEnter (EnableCol : Collider)
{
	_enableVehicleEnter = true;
}

function OnTriggerExit (DisableCol : Collider)
{
	_enableVehicleEnter = false;
}

I’ve been searching around for a little while and testing a few things, but haven’t had any luck so far. Can anyone help?

var mouseOrbit = cam.GetComponent("MouseOrbit");
mouseOrbit.target = boatVehicle;

Thanks very much, i found a similar solution, i just didn’t think that far as to think that the line should be

mouseOrbit.target = boatVehicle;

instead of

mouseOrbit.target = target;

I feel so stupid :smile: Ahh well i’m still a bit of a noob to this scripting :wink:

Hmm… for some reason i’m getting this error

The code looks like this now
I tried moving the line around a bit, nothing changes though :confused:

#pragma strict

//Defining the target of my MouseOrbit Script
var MouseOrbitScript: MouseOrbit = GameObject.Find("Camera").GetComponent("MouseOrbit");


var _enableVehicleEnter = false;

var isControllingPlayer = true;
var isControllingBoatVehicle = false;

//Transforms
var cam : Transform;
var player : Transform;
var boatVehicle : Transform;

function Start () 
{
	MouseOrbitScript.target = player;
	cam.GetComponent("MouseOrbit");
	
	//Make sure we are starting with controlling the player
	isControllingPlayer = true;
}

function Update () 
{
	//Enters the boat if button is pushed and if we are inside the collider
	if (_enableVehicleEnter == true  Input.GetButton("EnterVehicle"))
	{
		isControllingBoatVehicle = true;
	}

	//Make sure vehicleController and playerController can't be active at the same time
	
	
	//If we are controlling the player

	
	//If we are controlling the boat
	if (isControllingBoatVehicle == true)
	{
		MouseOrbitScript.target = boatVehicle;
	}
}



function OnTriggerEnter (EnableCol : Collider)
{
	_enableVehicleEnter = true;
}

function OnTriggerExit (DisableCol : Collider)
{
	_enableVehicleEnter = false;
}

I fixed it by putting

var MouseOrbitScript: MouseOrbit = GameObject.Find("Camera").GetComponent("MouseOrbit");

in both start and update functions

This should work fine

    #pragma strict
     
    //Defining the target of my MouseOrbit Script
    var MouseOrbitScript: MouseOrbit;
     
     
    var _enableVehicleEnter = false;
     
    var isControllingPlayer = true;
    var isControllingBoatVehicle = false;
     
    //Transforms
    var cam : Transform;
    var player : Transform;
    var boatVehicle : Transform;
     
    function Start ()
    {
        MouseOrbitScript = cam.GetComponent("MouseOrbit");

        MouseOrbitScript.target = player;
       
        //Make sure we are starting with controlling the player
        isControllingPlayer = true;
    }
     
    function Update ()
    {
        //Enters the boat if button is pushed and if we are inside the collider
        if (_enableVehicleEnter == true  Input.GetButton("EnterVehicle"))
        {
            isControllingBoatVehicle = true;
        }
     
        //Make sure vehicleController and playerController can't be active at the same time
       
       
        //If we are controlling the player
     
       
        //If we are controlling the boat
        if (isControllingBoatVehicle == true)
        {
            MouseOrbitScript.target = boatVehicle;
        }
    }
     
     
     
    function OnTriggerEnter (EnableCol : Collider)
    {
        _enableVehicleEnter = true;
    }
     
    function OnTriggerExit (DisableCol : Collider)
    {
        _enableVehicleEnter = false;
    }