What can possibly be wrong?

Hello!
I’m working on my studies and I came up with this problem I cant solve. So I have this small scene with a player tagged Astro and a car. I need to get it inside the car and switch the controls and cameras. The script is easy and should work as the Debug.Log showed it’s just fine.

But as I get near the car and try to get the script working, it just doesnt do anything. Only the Start void works when i start the game. I have seen many people doing almost the same script and It works.
Everything is assigned, console has no messages, car and player have colliders, rigidbodies and so on, separately they work just fine! The script is this one:

using UnityEngine;

public class GetInCar : MonoBehaviour
{
    public GameObject car, player, dummyInCar, lights, cam;
    private bool inCar = false;
    WheelController driveScript;
    CarCamera camScript;

    private void Start() {
        driveScript = GetComponent<WheelController>();
        camScript = GetComponent<CarCamera>();
        driveScript.enabled = false;
        camScript.enabled = false;
        
    }

    public void OnTriggerStay(Collider other) {
        if(other.CompareTag("Astro") && inCar == false && Input.GetKeyUp(KeyCode.E)) {
            dummyInCar.SetActive(true);
            player.SetActive(false);
            lights.SetActive(true);
            cam.SetActive(true);
            driveScript.enabled = true;
            camScript.enabled = true;
            player.transform.parent = car.transform;
            inCar = true;
        }
    }

    private void Update() {
        if(inCar == true && Input.GetKeyUp(KeyCode.E)) {
            player.SetActive(true);
            lights.SetActive(false);
            dummyInCar.SetActive(false);
            cam.SetActive(false);
            player.transform.parent = null;
            driveScript.enabled = false;
            camScript.enabled = false;
            inCar = false;
        }
    }

}

Every programmer comes to this same block wall, eventually. And every one will tell you “Debug”, lol. So start with the way you know should be the very first thing that happens, then progress to each new thing that may be wrong.

So personally I would re-write it as follows:

public void OnTriggerStay(Collider other) 
{
    print($"collision with {other} has been detected");

    if (other.CompareTag("Astro"))
        print("other tag is Astro");

    if (!inCar)
        print("inCar is equal to false");
// always use get key down
    if(other.CompareTag("Astro") && !inCar && Input.GetKeyDown(KeyCode.E)) 
    {
             dummyInCar.SetActive(true);
             player.SetActive(false);
             lights.SetActive(true);
             cam.SetActive(true);
             driveScript.enabled = true;
             camScript.enabled = true;
             player.transform.parent = car.transform;
             inCar = true;
         }
     }

^ In this case I checked all of them at once, but you should hopefully understand the point.

  • Also, do not leave print or Debug.Log within your code, it severely impacts performance. Just use as an “As needed basis” :slight_smile: