So I have a script for getting into a car which, when mouse down is detected on the given game object it will teleport the player to the car, turn of the player's controller, and switch the player camera to the car camera.
The problem is I don't want to have to drag and drop the car game object, the player game object, and both cameras into the inspector for it to work.
Instead I would like it to have the script assign all those game objects based on the player who clicked on the car. Those game objects being the car the player, and the two cameras(one parented to the player and the other parented to the car). I have no clue how to go about this so I appreciate any help all the more.
Here is a picture of the inspector with the player script:
Also here is my C# code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetInCar : MonoBehaviour {
public GameObject player;
public GameObject car;
public Camera CarCam;
public Camera PlayerCam;
void Start ()
{
}
void OnMouseDown ()
{
if (Input.GetKey (KeyCode.Mouse0))
{
player.transform.position = car.transform.position;
print("MovedToCar");
GameObject.Find ("Player").GetComponent<CharacterController> ().enabled = false;
PlayerCam.enabled = false;
CarCam.enabled = true;
}
}
}