- Home /
Can somebody see the error(s) here ? C# Scripting
Hey, i have this script. It is about entering and exiting cars. I have many cars in the project, every car has this script attached, and configured. But i enter this car, then i exit it, everything works fine, but after i drive, and enter/exit few cars, script gets crazy - as i - enter one car, when i pres the 'exit key', instead of exiting a car, i end up driving another, then press again exit - i end up driving two cars in the same time....weird. This is the script:
using UnityEngine;
using System.Collections;
public class EnterExit : MonoBehaviour {
public Drivetrain Drivetrain;
public CarController CarController;
public SoundController SoundController;
public GameObject Car;
public GameObject ExitPoint;
public GameObject CarCamera;
public GameObject Player ;
public GameObject PlayerCamera;
bool IsPlayerInCar = false;
bool IsPlayerInTrigger = false;
// Use this for initialization
void Start () {
CarController = GetComponent (typeof (CarController)) as CarController;
SoundController = GetComponent (typeof (SoundController)) as SoundController;
}
void OnTriggerEnter(Collider Player) {
IsPlayerInTrigger = true;
}
void OnTriggerExit(Collider Player) {
IsPlayerInTrigger = false;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.F)) {
if (!IsPlayerInCar && !IsPlayerInTrigger)
{
// Actions to perform when not in car, and not in trigger
}
if (!IsPlayerInCar && IsPlayerInTrigger)
{
// Actions to perform when not in car but in car's trigger
CarController.enabled = true;
Drivetrain.enabled = true;
CarCamera.camera.enabled = true;
CarCamera.gameObject.GetComponent<AudioListener> ().enabled = true;
PlayerCamera.camera.enabled = false;
PlayerCamera.gameObject.GetComponent<AudioListener> ().enabled = false;
Player.gameObject.transform.parent = ExitPoint.transform;
Player.transform.localPosition = new Vector3(0,0,0);
Player.transform.localRotation = new Quaternion(0,0,0,0);
Player.gameObject.SetActive(false);
StartCoroutine(EnterCar());
}
if (IsPlayerInCar)
{
// Actions to perform when in car (to exit car)
CarController.brake = 999;
CarController.enabled = false;
Drivetrain.enabled = false;
CarCamera.camera.enabled = false;
CarCamera.gameObject.GetComponent<AudioListener> ().enabled = false;
PlayerCamera.camera.enabled = true;
PlayerCamera.gameObject.GetComponent<AudioListener> ().enabled = true;
Player.gameObject.transform.parent = null;
Player.transform.localRotation = new Quaternion(0,0,0,0);
Player.gameObject.SetActive(true);
StartCoroutine(ExitCar());
}
}
}
IEnumerator EnterCar()
{
yield return new WaitForSeconds(0.5f);
IsPlayerInCar = true;
SoundController.EngineOn = true;
}
IEnumerator ExitCar()
{
yield return new WaitForSeconds(1);
IsPlayerInCar = false;
SoundController.EngineOn = false;
}
}
I haven't read through the entire script, but I noticed you named it EnterExit. Generally speaking, na$$anonymous$$g (and further modeling) a class after an action is a bad idea and can lead to organizational and conceptual issues. Why can't the enter and exit methods be a part of the greater car class? It stands to reason that some of your issues comes from the way you are separating the concerns of the car functionality.
For what its worth I would say that na$$anonymous$$g a class as a behaviour/action is not only recommended but also reflected by Unity's model seeing as the classes are inherited from a Behaviour-based class ($$anonymous$$onoBehaviour). A $$anonymous$$onoBehaviour class, the way I see it, is a way for a Unity user to define specialized behaviours for each of their game objects. Beyond that, I would say the problem is most likely do with the way you're using coroutines in the Update() method. I'd think that because you're not updating the behaviour state for X second (and Update() is still called for a few times) you have more coroutines created on the object; I think anyway, not having run the code myself. $$anonymous$$y suggestion, create a 'UseCar' behaviour and attach it to the player which is triggered when the right requirements are met (player is next to a car and player pressed correct input).
I am quite new to scripting, my first if(){} was written about 3 months ago, and till than i had no contact with any scripting language. So i dont quite understand how behaviours should and be used. Do you have any tutorials to guide me to ? Thanks!