Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Fixed

Discussion in 'Scripting' started by ShaunChad99, May 11, 2020.

  1. ShaunChad99

    ShaunChad99

    Joined:
    Apr 23, 2020
    Posts:
    26
    i have just down this script it works fine until i want to get back in after exiting it doesn't work
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using VehicleControles;
    5.  
    6.     public class EnterExit : MonoBehaviour
    7.     {
    8.         public GameObject Car;
    9.         public GameObject CarCam;
    10.         public GameObject EnterText;
    11.         public GameObject Crosshair;
    12.         public GameObject Player;
    13.         public GameObject playerInCar;
    14.  
    15.         private bool inVehicle = false;
    16.  
    17.         void Start()
    18.         {
    19.             playerInCar.SetActive(false);
    20.             CarCam.SetActive(false);
    21.             EnterText.SetActive(false);
    22.             gameObject.GetComponent<Vehicle>().enabled = false;
    23.         }
    24.  
    25.         void OnTriggerStay(Collider other)
    26.         {
    27.             if (other.gameObject.tag == "Player" && inVehicle == false)
    28.             {
    29.                 EnterText.SetActive(true);
    30.             }
    31.             if (other.gameObject.tag == "Player" && inVehicle == false && Input.GetKeyDown("e"))
    32.             {
    33.                 Player.SetActive(false);
    34.                 EnterText.SetActive(false);
    35.                 Crosshair.SetActive(false);
    36.                 playerInCar.SetActive(true);
    37.                 CarCam.SetActive(true);
    38.                 Player.transform.parent = Car.transform;
    39.                 gameObject.GetComponent<Vehicle>().enabled = true;
    40.             inVehicle = true;
    41.             }
    42.         }
    43.  
    44.         void OnTriggerExit(Collider other)
    45.         {
    46.             if (other.gameObject.tag == "Player")
    47.             {
    48.                 EnterText.SetActive(false);
    49.             }
    50.         }
    51.  
    52.         void Update()
    53.         {
    54.             if (inVehicle == true && Input.GetKeyDown("f"))
    55.             {
    56.                 playerInCar.SetActive(false);
    57.                 CarCam.SetActive(false);
    58.                 gameObject.GetComponent<Vehicle>().enabled = false;
    59.                 Player.transform.parent = null;
    60.                 Player.SetActive(true);
    61.                 Crosshair.SetActive(true);
    62.             }
    63.         }
    64.     }
     
  2. pinksheep8426

    pinksheep8426

    Joined:
    Jul 2, 2016
    Posts:
    5
    You never set inVehicle to false