Home > Software design >  Unity enable/disable component script from component on same gameObject
Unity enable/disable component script from component on same gameObject

Time:02-15

Preamble

I am currently making a game where a player can go from third person view, walking around to transition into a vehicle.

I have thought about using a transmitter/receiver type set up, but I think my way of simplifying it isn't correct.

I am using assets from third parties for controllers that use inputs. My plan was to enable/disable the appropriate script and camera from the object I want to control. I've gotten as far as being able to disable the previous controller and enable the next, though I can't go back to enabling since the script obviously doesn't run anymore.

Question/Request

I'd like to be able to reference the pawn's input component script in a different component script on the same gameobject to then be able to enable/disable the aformentioned component, though the issue is that the input controllers have variable names (Different names depending on the third party on each pawn).

Here's how I have it set up: I have a PlayerTransmitter that handles the basics of turning things on and off. I tried making this where all of the inputs are being handled, but I don't want to have to change the original controller scripts to look at this script. This is on an empty game object and handles the 'state' of the player, (walking or in which vehicle).

enter image description here

on each pawn gameobject (The walking pawn CleanerPawn and the vehicle TractorPawn), I've added a script called InputReceiver. This was originally intended to pass the inputs from the PlayerTransmitter to the actual controller on the object itself.

Right now, the walking pawn has a component called AdvancedWalkerController (You may know the one I'm talking about) that controls the player walking movement and the vehicle has a component called VehicleController which controls how the vehicle moves and handles.

CleanerPawn

enter image description here

TractorPawn

enter image description here

The two images above show that I am using the same InputReceiver component on both pawns. My plan was to pass in each pawn's input controller (temporarily named CleanerController) and then enable/disable that input controller depending on the PlayerTransmitter 'state'.

The InputReceiver currently looks like this:

    public bool isEnabled;
    public Component CleanerController;

    public void TurnOffControls()
    {
        isEnabled = false;
    }

    public void TurnOnControls()
    {
        isEnabled = true;
    }

In each pawn's input controller, I added a line to the Update() function: enabled = gameObject.GetComponent<InputReceiver>().isEnabled;. Two problems with this; 1. I don't want to change any code in the input scripts (I hope this is possible) and 2. Once it's disabled, it won't read an update to enable itself again.

I was hoping I could just use the reference to the component CleanerController to say CleanerController.enabled = true; or false with that reference being just that one component, though I'm missing something here.

My final thought which I am going to try is to allow/disallow the input controls within each input script depending on my isEnabled boolean. Though again, I would have to change the original scripts to accommodate this.

CodePudding user response:

I would recommend you to have a Controller Script. This named "CameraController" for example and has a Method to switch between the Modes. This would also make it possible to have even more Options. For Example you could use an Enum to define which modes exist:

enum CameraMode {
    Player,
    Car
}

public SwitchMode(CameraMode mode) {
    switch(mode) {
        case CameraMode.Player:
            TurnOnControls();
            break;
        case CameraMode.Car:
            TurnOffControls();
            break;
        default:
            throw new NotSupportedException();
    }
}

You could even go further and create an Interface/Abstract which allows all of this and just has an execute method doing different stuff. In the end you just need that controller which allowes to swtich between the modes and handles the key input.

CodePudding user response:

What speaks against

public Behaviour CleanerController;

and then

CleanerController.enabled = false; // or true accordingly

See

  • Behaviour

    Behaviours are Components that can be enabled or disabled.

  • Behaviour.enabled

    Enabled Behaviours are Updated, disabled Behaviours are not.

    or in other words: If a Behaviour is set to enabled = false then it's Update (and similar event methods) is not called anymore.

  • Related