• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JoeD86 · Aug 11, 2020 at 10:15 AM · uieventeventsystembackend

Event added from AddListener on a button not always being called

In my app, I have a button that allows you to speed up the process of any operation such as constructing a building and researching new technology. The way it works is you tap the speed up button, it makes a call to the back end asking for how much time is remaining and credit cost, and once it's received you are given a question asking to confirm the speed up. If you say yes, it's supposed to call a method in the backend that applies the speed up. It works fine for structures, but not research. Here are snippits of the code.

First, when you tap the first speed up button.

 virtual protected void OnSpeedUpClick()
     {
         SoundManager.instance.beep.Play();
 
         string planetId = PlayerManager.instance.planets[GameManager.instance.chosenPlanetID].id;
         if (prefabType == C_ActionType.BUILDINGS)
         {
 //            NetworkManager.instance.ConstructBuilding(PlayerManager.instance.SessionToken, NetworkManager.instance.gameId, "1", planetId, building.Id.ToString(), 1, OnUpgCallback);
 
             NetworkManager.instance.SpeedUp(PlayerManager.instance.SessionToken, NetworkManager.instance.gameId, "1", building.QueueId, "buildings", "0", SpeedUpConfirmResponse);
         }
         if (prefabType == C_ActionType.UPGRADES)
         {
             NetworkManager.instance.SpeedUp(PlayerManager.instance.SessionToken, NetworkManager.instance.gameId, "1", upgrade.QueueId, "shipsupgrades", "0", SpeedUpConfirmResponse);
         }
         if (prefabType == C_ActionType.SCIENCES)
         {
             NetworkManager.instance.SpeedUp(PlayerManager.instance.SessionToken, NetworkManager.instance.gameId, "1", science.QueueId, "sciences", "0", SpeedUpConfirmResponse);
         }
         if (prefabType == C_ActionType.SHIPS)
         {
             NetworkManager.instance.SpeedUp(PlayerManager.instance.SessionToken, NetworkManager.instance.gameId, "1", ship.QueueId, "ships", "0", SpeedUpConfirmResponse);
         }
         if (prefabType == C_ActionType.DEFENCES)
         {
             NetworkManager.instance.SpeedUp(PlayerManager.instance.SessionToken, NetworkManager.instance.gameId, "1", defence.QueueId, "defence", "0", SpeedUpConfirmResponse);
         }
 
     }


Here is the response

     private void SpeedUpConfirmResponse(JsonData response)
     {
 
         GameManager.instance.loadingSpinnyThing.SetActive(false);
 
         speedUpEvent.RemoveListener(RequestSpeedUp);
         speedUpEvent.AddListener(RequestSpeedUp);
 
         Debug.Log(response["message"].ToString());
         if(response["message"].ToString().Contains("Process not found"))
         {
             ActionViewController.instance.hideSpeedUp.Add(id);
         }
 
         GameManager.instance.EnableDynamicView(response["message"].ToString(), "speedUp", speedUpEvent);
 
         ActionViewController.instance.SetResources();
         ActionViewController.instance.GetInfo();
     }

Everything leading to the creation of the Textbox

 (In GameManager.cs)
 
     public void EnableDynamicView(string text, string type, UnityEvent e = null)
     {
         dynamicView.SetActive(true);
         dynamicView.GetComponent<DynamicViewController>().SetFunctionality(text, type, e);
     }
 
 (
 
 (In DynamicView.cs)
 
 public void SetFunctionality(string txt, string type, UnityEvent e = null)
     {
 
         this.text = txt;
         this.type = type;
 
         textUI.text = text;
         leftBtnUI.gameObject.SetActive(true);
         rightBtnUI.gameObject.SetActive(true);
         leftBtnUI.onClick.RemoveAllListeners();
         rightBtnUI.onClick.RemoveAllListeners();
         SoundManager.instance.comm.Play();
         Debug.Log(txt);
 
 
         switch (this.type)
         {
             case "logout":
                 leftBtnUI.onClick.AddListener(() => LogoutAccept());
                 rightBtnUI.onClick.AddListener(() => LogoutCancel());
                 leftBtnUI.transform.GetChild(0).GetComponent<Text>().text = "Yes";
                 rightBtnUI.transform.GetChild(0).GetComponent<Text>().text = "No";
                 break;
             case "noConnection":
                 leftBtnUI.onClick.AddListener(() => RetryConnection());
                 rightBtnUI.onClick.AddListener(() => CloseGame());
                 leftBtnUI.transform.GetChild(0).GetComponent<Text>().text = "Retry";
                 rightBtnUI.transform.GetChild(0).GetComponent<Text>().text = "Close";
                 break;
             case "speedUp":
                 leftBtnUI.onClick.AddListener(() => e.Invoke());
                 leftBtnUI.onClick.AddListener(() => disableView());
                 rightBtnUI.onClick.AddListener(() => disableView());
                 leftBtnUI.transform.GetChild(0).GetComponent<Text>().text = "Yes";
                 rightBtnUI.transform.GetChild(0).GetComponent<Text>().text = "No";
                 break;
             case "notEnoughResources":
                 leftBtnUI.onClick.AddListener(() => disableView());
                 rightBtnUI.gameObject.SetActive(false);
                 leftBtnUI.transform.GetChild(0).GetComponent<Text>().text = "OK";
                 rightBtnUI.transform.GetChild(0).GetComponent<Text>().text = "Close";
                 break;
             case "oneButtonError":
                 rightBtnUI.gameObject.SetActive(false);
                 leftBtnUI.onClick.AddListener(() => disableView());
                 //rightBtnUI.onClick.AddListener(() => disableView());
                 leftBtnUI.transform.GetChild(0).GetComponent<Text>().text = "OK";
                 //rightBtnUI.transform.GetChild(0).GetComponent<Text>().text = "Close";
                 break;
             default:
                 leftBtnUI.onClick.AddListener(() => disableView());
                 rightBtnUI.onClick.AddListener(() => disableView());
                 leftBtnUI.transform.GetChild(0).GetComponent<Text>().text = "OK";
                 rightBtnUI.transform.GetChild(0).GetComponent<Text>().text = "Close";
                 break;
 
         }
         if (txt.Contains("500 Internal Server Error"))
         {
             // if(ActionViewController.instance.gameObject.activeInHierarchy)
             ActionViewController.instance.closeActionView();
 
         }
     }
 
 

Finally, here is the event that is supposed to be getting called.

     private void RequestSpeedUp()
     {
 
 
         if (prefabType == C_ActionType.BUILDINGS)
         {
             NetworkManager.instance.SpeedUp(PlayerManager.instance.SessionToken, NetworkManager.instance.gameId, "1", building.QueueId, "buildings", "1", SpeedUpResponse);
         }
         if (prefabType == C_ActionType.UPGRADES)
         {
             NetworkManager.instance.SpeedUp(PlayerManager.instance.SessionToken, NetworkManager.instance.gameId, "1", upgrade.QueueId, "shipsupgrades", "1", SpeedUpResponse);
         }
         if (prefabType == C_ActionType.SCIENCES)
         {
 
             NetworkManager.instance.SpeedUp(PlayerManager.instance.SessionToken, NetworkManager.instance.gameId, "1", science.QueueId, "sciences", "1", SpeedUpResponse);
         }
         if (prefabType == C_ActionType.SHIPS)
         {
             NetworkManager.instance.SpeedUp(PlayerManager.instance.SessionToken, NetworkManager.instance.gameId, "1", ship.QueueId, "ships", "1", SpeedUpResponse);
         }
         if (prefabType == C_ActionType.DEFENCES)
         {
             NetworkManager.instance.SpeedUp(PlayerManager.instance.SessionToken, NetworkManager.instance.gameId, "1", defence.QueueId, "defence", "1", SpeedUpResponse);
         }
     }



And the response up to the sciences, so this post doesn't get more cluttered.

 private void SpeedUpResponse(JsonData response)
     {
 
         GameManager.instance.loadingSpinnyThing.SetActive(false);
         if (!response.Keys.Contains("success") || (bool)response["success"] == false)
         {
 
             Debug.Log("Something went wrong with speed up building");
             GameManager.instance.EnableDynamicView("Something went wrong.", "oneButtonError");
             return;
         }
         else
         {
 
             speedUpDelay = 5f;
             if (prefabType == C_ActionType.BUILDINGS)
             {
                 if(PlayerManager.instance.buildings == null)
                 {
                     Debug.Log("Calling refresh");
                    // ActionViewController.instance.pushedRefresh();
                 }
                 for (int i = 0; i < PlayerManager.instance.buildings.Length; i++)
                 {
                     if (id == PlayerManager.instance.buildings[i].Id)
                     {
                         PlayerManager.instance.buildings[i].InProgress = false;
                         PlayerManager.instance.buildings[i].CanUpgrade = true;
 
                         //JsonData elementInfo = response["process"];
                         //PlayerManager.instance.buildings[i].Level = Int32.Parse(elementInfo["level"].ToString());
                         SetElement();
                     }
                 }
             }
             else if (prefabType == C_ActionType.UPGRADES)
             {
                 for (int i = 0; i < PlayerManager.instance.upgrades.Length; i++)
                 {
                     if (id == PlayerManager.instance.upgrades[i].Id)
                     {
                         PlayerManager.instance.buildings[i].InProgress = false;
                         //PlayerManager.instance.buildings[i].CanUpgrade = true;
 
                         //JsonData elementInfo = response["process"];
                         //PlayerManager.instance.buildings[i].Level = Int32.Parse(elementInfo["level"].ToString());
                         SetElement();
                     }
                 }
             }
             else if (prefabType == C_ActionType.SCIENCES)
             {
 
                 for (int i = 0; i < PlayerManager.instance.sciences.Length; i++)
                 {
                     if (id == PlayerManager.instance.sciences[i].Id)
                     {
 
                         PlayerManager.instance.buildings[i].InProgress = false;
                         //PlayerManager.instance.buildings[i].CanUpgrade = true;
 
                         //JsonData elementInfo = response["process"];
                         //PlayerManager.instance.buildings[i].Level = Int32.Parse(elementInfo["level"].ToString());
                         SetElement();
                         ActionViewController.instance.pushedRefresh();
                     }
                 }
             }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

226 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hide/show UI Button and trigger the OnPointerDown event ?! 0 Answers

Get Global PointerEventData 1 Answer

Event system or UI Button ? 0 Answers

Is reference better than UnityEvent ? 1 Answer

Can't fire a Custom Event on UI Button Touch OnClick 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges