- Home /
When my button is pressed the action reapeats
I'm making a gun customization scene for my game and i'm making is so that when you press a button some more buttons show up but when i try to click one of my buttons (They are planes not Gui) it starts repeating either the "BackButton" or the "MainButotn4". When I click on a button while "MainButton4" is repeating "MainButton2" flashes then 4 keeps repeating. Anyone know how I can make it so that when one button is pressed it stays pressed and doesn't repeat. Here are me scripts:
Game Logic:
var MainSightID : int = 0;
var MainMuzzleID : int = 0;
var MainGuns : int = 0;
var MainSubButtons : int = 0;
function Update () {
}
Button Logic:
var SubButtons : int = 0;
var AssultRifleACWR : GameObject;
var PistolM9 : GameObject;
var ShotgunMCS : GameObject;
var SniperBarret : GameObject;
var GameLogicObj : GameObject;
function Awake(){
GameLogicObj = GameObject.FindGameObjectWithTag("Game_Logic");
}
function Update () {
Debug.Log (SubButtons);
SubButtons = GameLogicObj.GetComponent("Game_Logic").MainSubButtons;
if (SubButtons == 0){
AssultRifleACWR.SetActive(false);
PistolM9.SetActive(false);
ShotgunMCS.SetActive(false);
SniperBarret.SetActive(false);
}
if (SubButtons == 1){
AssultRifleACWR.SetActive(true);
PistolM9.SetActive(false);
ShotgunMCS.SetActive(false);
SniperBarret.SetActive(false);
}
if (SubButtons == 2){
AssultRifleACWR.SetActive(false);
PistolM9.SetActive(true);
ShotgunMCS.SetActive(false);
SniperBarret.SetActive(false);
}
if (SubButtons == 3){
AssultRifleACWR.SetActive(false);
PistolM9.SetActive(false);
ShotgunMCS.SetActive(true);
SniperBarret.SetActive(false);
}
if (SubButtons == 4){
AssultRifleACWR.SetActive(false);
PistolM9.SetActive(false);
ShotgunMCS.SetActive(false);
SniperBarret.SetActive(true);
}
}
Button Script:
var GameLogicObj : GameObject;
var ButtonID : String = "";
function Awake(){
GameLogicObj = GameObject.FindGameObjectWithTag("Game_Logic");
}
function OnMouseEnter (){
renderer.material.color = Color.gray;
}
function OnMouseExit (){
renderer.material.color = Color.white;
}
function Update (){
if(Input.GetMouseButtonDown(0)){
if(ButtonID == "BackButton"){
GameLogicObj.GetComponent("Game_Logic").MainSubButtons = 0;
}
}
if(Input.GetMouseButtonDown(0)){
if(ButtonID == "MainButton1"){
GameLogicObj.GetComponent("Game_Logic").MainSubButtons = 1;
}
}
if(Input.GetMouseButtonDown(0)){
if(ButtonID == "MainButton2"){
GameLogicObj.GetComponent("Game_Logic").MainSubButtons = 2;
}
}
if(Input.GetMouseButtonDown(0)){
if(ButtonID == "MainButton3"){
GameLogicObj.GetComponent("Game_Logic").MainSubButtons = 3;
}
}
if(Input.GetMouseButtonDown(0)){
if(ButtonID == "MainButton4"){
GameLogicObj.GetComponent("Game_Logic").MainSubButtons = 4;
}
}
}
Answer by srancsik · Sep 30, 2013 at 08:41 AM
I think the problem is that in Button script you have the MainButton scripts in the function Update, which continues to run this script frame by frame (causing the flashing). Try put the whole content of the Button script function Update to funtion OnMouseEnter.
I have tried that too (sorry I forgot to put it on my question) but when i do that I have to click outside the editor everytime a button is pressed.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
A node in a childnode? 1 Answer
Switch Camera On Button Press? 2 Answers
Loop animation on button hold 2 Answers
how to run Sprint 1 Answer