How to disable scripts of the gameobject the camera is targeting?

Hi everyone,

Below is the code that is attached to the vehicle game object. So whenever the player goes inside a vehicle in this instance the camera target “cartarget” is set to the vehicle object the player enters.

CarCam.GetComponent<CarCam>().cartarget = this.gameObject.transform;

Is there a way I can disable scripts of the gameobject the camera is only targetting? so the scripts of the “cartarget variable” object?

Wish to do something like below but only for the object the camera is targetting so other vehicles have same script so don’t want to activate/deactive scripts for other cars.

  gameObject.GetComponent<CarController>().enabled = false;

Thanks.

At the point where you tell the camera script to target a game object, you can use something like GetComponents() on that gameobject, then turn all those components off.

You may wish to look into interfaces for this, such that you have scripts that you WANT to turn off explicitly implement an interface, such that other scripts on the car that should NOT be turned off can choose to not implement that interface, then they will keep running.

is is possible to combine to one line of code?

CarCam.GetComponent<CarCam>().cartarget = this.gameObject.transform.GetComponent<CarController>().enabled = false;

Now why on EARTH would you want to do this to yourself?!

It is 2021… lines of code are FREE… take all you want.

Just say no to “Hairy Lines Of Code.”

How to break down hairy lines of code:

http://plbm.com/?p=248

Hairy lines of code are where bugs come from, horrible project destroying bugs.

1 Like

I can’t wait for the day you ask a complex question I know the answer to, just so I can write it as 1 giant line of code :stuck_out_tongue:

1 Like