How to Activate/Deactivate Component via Inputs

Hi guys,
I need to Activate and Deactivate a component (an external script) that is attached to player’s camera via gamepad input.
Basically it’s a glitch effect that I need to activate with one button (ex. L2) and deactivate with another button (ex. R2)
Can’t find any (noob) method…

Thanks!

Depends which controller you have. There are some Schematics over the Internet so you could do something like this below. (X-BoxOne, PS4)

What @Twistyd explained was the Method to a UI Button not a GamePad button/input.


    bool m_Switch;
	Kino.Datamosh k_data;

	void Update () {
		k_data = Camera.main.GetComponent<Kino.Datamosh>();


		//there are schematics of GamePad Layouts in the web. 
		//Left back button(L1) for xboxOne / ps4 i believe 
		if(Input.GetKeyDown(KeyCode.JoystickButton4)){
			Camera.main.GetComponent<Kino.Datamosh>().enabled = true;
		}
		//right back button(R1) for xboxOne / ps4
		if(Input.GetKeyDown(KeyCode.JoystickButton5)){
			Camera.main.GetComponent<Kino.Datamosh>().enabled = false;
		}



		// if a single button should enable/disable component
		if(Input.GetKeyDown(KeyCode.JoystickButton4)){
			m_Switch = !m_Switch;
		}
		if(m_Switch){
			k_data.enabled = true;
		}
		if(!m_Switch){
			k_data.enabled = false;
		}
	}

Few ways you could do it.

You could write 2 public functions which handle the activation and deactivation of the effect.

Something like:

    public GameObject effectThing;     // Your effect object.
    
    public void Activation(){
        effectThing.SetActive(true);
    }
    
    public void Dectivation(){
        effectThing.SetActive(false);
    }    

Then back in Unity drag whichever object contains the functions in the script above in to your buttons onClick

127898-screenshot-4.jpg


Or you could directly drag in the effectThing GameObject to the onClick and then in the bit that says no function select GameObject and then SetActive (bool)

127901-screenshot-5.jpg

I too am fairly nooby so there may be better ways to do it.

thanks for you quick reply!
I don’t understand with this method (I didn’t know it) how I set Up a custom Key for the action.
The effect that I need to control it’s on my main camera, so this is what I’ve done:

Created a Game Object and put there the button script
then assign on OnClick my main camera and in function I can see my effect (datamosh) with all the settings!
but I don’t understand where I have to assign buttons to settings!
Thanks!

Unfortunately I’ve tried to make a Build, everything works fine but the Datamosh effect,
when I press L1 the camera freezes on a frame until I press it again. basically just stops the frame and no datamosh effect applied!

Seeing the debug in the build while pressing the button to activate the datamosh component it gives me this error:
NullReferenceException: Object reference not set to instance of an object

It dosn’t pop up in unity in the game mode!