Access other gameObject's components in C# script

Hey, I’m trying to create a character switching function in my game, so when you aim at a character and press a key (‘x’) in this case, you assume control of the character you aimed at and the camera moves to the other character.

So far, I’ve setup a basic movement and aiming script, and casts a raycast so when you are aiming at another character, it changes colour (A placeholder function).

I’m wanting to have the PlayerSwitch script (Which is located in the other character) enable in the Input.GetKey(‘x’) function.

Here’s my script:

using UnityEngine;
using System.Collections;

[RequireComponent (typeof(CharacterController))]
public class FirstPersonController : MonoBehaviour {

    public float movementSpeed = 5.0f;
    public float mouseSensitivity = 5.0f;
    public float jumpSpeed = 20.0f;
    public float aimedSpeed = 2.5f;
    private GameObject Ped1;

    float verticalRotation = 0;
    public float upDownRange = 30.0f;

    float verticalVelocity = 0;

    CharacterController characterController;

    // Use this for initialization
    void Start () {
        Screen.lockCursor = true;
        transform.Translate(new Vector3(3,3,1));
        characterController = GetComponent<CharacterController>();
        Ped1 = GameObject.Find("Ped1");

    // Update is called once per frame
    void Update () {
        // Rotation

        float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
        transform.Rotate(0, rotLeftRight, 0);

        verticalRotation -= Input.GetAxis("Mouse Y") * mouseSensitivity;
        verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
        Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);

        // Movement

        float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
        float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;

        verticalVelocity += Physics.gravity.y * Time.deltaTime;

        if( characterController.isGrounded && Input.GetButton("Jump") ) {
            verticalVelocity = jumpSpeed;

        Vector3 speed = new Vector3( sideSpeed, verticalVelocity, forwardSpeed );

        speed = transform.rotation * speed;

        characterController.Move( speed * Time.deltaTime );

                Camera.main.transform.Translate(new Vector3(0,0,2));
                movementSpeed = 2.5f;

            if(Input.GetMouseButtonUp (1)){
            Camera.main.transform.Translate (new Vector3(0,0,-2));
                movementSpeed = 5.0f;

                movementSpeed = 10.0f;
            if(Input.GetButtonUp ("Sprint")){
            movementSpeed = 5.0f;

        Ray rayOrigin = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
        RaycastHit hitInfo;
            if(Physics.Raycast (rayOrigin, out hitInfo, 10f) && hitInfo.transform.tag == "Pedestrian"){

                print ("You are casting a ray");
                Debug.DrawRay (rayOrigin.direction, hitInfo.point);
                hitInfo.collider.gameObject.renderer.material.color =;

            if(Input.GetKey ("x")){
                 gameObject.GetComponent ("PlayerSwitch");


There’s a whole page dedicated to this question in the manual. You know the gameobject on which to call the script from hitInfo.collider.gameObject.