- Home /
[SOLVED] Disable / enable specific input button - call C# variable from js
EDIT 2: Problem solved. There's an adage called "Occam's razor," which essentially says "the simplest solution is the best one." So, since js is what I'm comfortable with, I simply reverse engineered my two C# scripts to it, then declared a variable. Once I did that, everything ran smoothly.
[Edit] Thanks to Meltdown and thaiscorpion there's been quite a bit of progress made on this since last night, but I'm asking for a bit more help to get over the hump. Long story short, pressing the "pause" button on a gamepad brings up the pause menu, naturally. When in a shop, however, I need to disable that. Have gotten done with that part, using a variable, but once out of the shop I need to reenable the pause button, and that's where I'm having issues. (actually, to be fair, the way I have the script(s) written up right now might be preventing it from working at all) There are three scripts involved: The pause script, which is simply a camera switch, the shop script, which is also a camera switch, and the shop inventory. The issue is the camera switches are both in C# (I know, I know) while the shop inventory is in js. I believe that I'm calling the variable correctly, which resides in C#, from the js script, and I have the scripts in the correct folders for compiling order, but think my syntax might be wrong, or something else that's preventing the variable from being set to false. Am hoping fresh eyes might spot what I'm doing wrong. Thanks, and God bless.
Script 1: Shop script
using UnityEngine;
using System.Collections;
public class Shopcam : MonoBehaviour {
public static bool _inShop = false; // <------- THIS VARIABLE
public Camera camera1;
public Camera camera2;
bool camera1ActiveBool;
void Start () {
camera1.camera.active = true;
camera2.camera.active = false;
camera1ActiveBool = true;
}
void OnTriggerEnter(Collider col) {
//use whatever button you want to toggle
_inShop = true;
if(col.gameObject.tag == "Player") {
if (camera1ActiveBool == true)
{
camera1.camera.active = false;
camera2.camera.active = true;
camera1ActiveBool = false;
Time.timeScale = 0;
}
}
}
}
Second Script
using UnityEngine; using System.Collections;
public class Menu : MonoBehaviour {
public Camera camera1;
public Camera camera2;
bool camera1ActiveBool;
void Start () {
camera1.camera.active = true;
camera2.camera.active = false;
camera1ActiveBool = true;
}
void Update () {
//use whatever button you want to toggle
if (Shopcam._inShop = false){
if(Input.GetButtonDown("Inventory")){
if (camera1ActiveBool == true)
{
camera1.camera.active = false;
camera2.camera.active = true;
camera1ActiveBool = false;
Time.timeScale = 0;
}
else if (camera1ActiveBool == false)
{
camera1.camera.active = true;
camera2.camera.active = false;
camera1ActiveBool = true;
Time.timeScale = 1;
}
}
}
}
}
Third Script Fragments (entire thing is TL:DR, and mostly irrelevant, I believe. This just includes the variable call
//in js
var csScript : Shopcam;
var SwitchCamera : GameObject;
function Start (){
csScript = SwitchCamera.GetComponent("Shopcam");
}
function SelectItemFromInventory()
{
if(itemPosition == 4)
{
csScript._inShop = true; // here's where I'm trying to change the variable in the C# script
}
[END OF EDIT]
Hello again, friends! I tried looking this up, and saw a couple of topics that were similar but not quite the same as I want to do. Essentially, I want to disable one gamepad button upon activation of a script. For instance: The player goes into a shop, and the "shop" script opens. In reality, the shop script is little more than my inventory script, which is attached to button "pause." (as I have it assigned in the input manager) So while the player is in the "shop" I'd like to disable the pause button, but no others, as they still need to navigate their selections and choose their purchases with another button. Does anyone know if this can be done? Thanks, and God bless.
You can try adding a variable in the code for when the player is in the shop, then in the code that gets executed when the button is pressed add an if statement like: if (playerInShop){ //Pause button code here }
Answer by Meltdown · Dec 03, 2013 at 12:12 AM
Use a boolean variable and disable the pause button if they're in the shop?
bool _inShop = false;
void Update()
{
// Code to set user as being in shop
_inShop = true;
// Code to handle input
if(Input.GetKey("Pause")
{
PauseGame();
}
}
void PauseGame()
{
if(_inShop)
return;
// Else do code here to pause game...
}
the idea is genius, and makes total sense. Thank you! Unfortunately I'm still in that noob stage of development where I haven't mastered how to call a variable or bool between a C# script and js, and right now my pause camera switch script is C# while my shop and pause menu inventories are both js. I will tuck this away until I learn how to use code between both standards. This makes sense as the right answer, I just need to learn how to apply it now. Thanks again!
Your answer
Follow this Question
Related Questions
Input.GetAxisRaw 1 Answer
Old Input System Gamepad Synced Controls 1 Answer
Problem with the gamepad in the new input system 2 Answers
Gamepad input, axis OR button 0 Answers
How to get smooth analog joystick rotation without "snapping"? 2 Answers