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!
Answer by dan_wipf · Nov 18, 2018 at 08:04 AM
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;
}
}
no just put this in an new script, and add it to the player, well basicly it doesnt matter where you’d put this
sorry dan, I'm very bad at it. wrote this: using System.Collections; using System.Collections.Generic; using UnityEngine;
bool m_Switch; $$anonymous$$ino.Datamosh k_data; void Update () { k_data = Camera.main.GetComponent<$$anonymous$$ino.Datamosh>();
//there are schematics of GamePad Layouts in the web.
//Left back button(L1) for xboxOne / ps4 i believe
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.JoystickButton4)){
Camera.main.GetComponent<$$anonymous$$ino.Datamosh>().enabled = true;
}
//right back button(R1) for xboxOne / ps4
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.JoystickButton5)){
Camera.main.GetComponent<$$anonymous$$ino.Datamosh>().enabled = false;
}
// if a single button should enable/disable component
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.JoystickButton4)){
m_Switch = !m_Switch;
}
if(m_Switch){
k_data.enabled = true;
}
if(!m_Switch){
k_data.enabled = false;
}
}
It gives me this error: Assets/scripts/NewBehaviourScript.cs(6,0): error CS1525: Unexpected symbol `$$anonymous$$ino'
ok if you create a new script
then put my code after: yourscriptname : monobehaviour {
delet all inside the brakets of the new script{} and add there the code
@dan_wipf is right, I gave a UI button answer, my bad, sorry, was late and I misread lol. Hope you get your issue sorted.
dude. works. perfectly. neat. crying loud, you saved me. many thanks guys! the effect is cool, thinking to implement with your fading effect for much coolness.
Answer by Twistyd · Nov 17, 2018 at 10:18 PM
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
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)
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 $$anonymous$$ey 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!
Answer by benjano · Nov 18, 2018 at 06:45 PM
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!
post a photo of the debug, if possible ir the whole debug log
it's just infinite lines of this: (Filename: /Users/Ben/Dropbox/mesh_depression/Assets/$$anonymous$$ino/Datamosh/Datamosh.cs Line: 161)
NullReferenceException: Object reference not set to an instance of an object at $$anonymous$$ino.Datamosh.OnRenderImage (UnityEngine.RenderTexture source, UnityEngine.RenderTexture destination) [0x00013] in /Users/Ben/Dropbox/mesh_depression/Assets/$$anonymous$$ino/Datamosh/Datamosh.cs:161