Not a great title for the thread but here is my issue:
I have 3 arrays of buttons that all used to work when i assigned them manually in the inspector. I got tired of doing this every level since i reworked my canvas after making all of the levels so I started assigning these in via code. I had the Vert Movement and UpsideDown Movement objects turned off and the code would turn it back on when appropriate triggers.
In order to assign via code I had to re-enable these so they could be found with GameObject.Find. Everything assigned properly as I mentioned but the LeftButton and RightButton will not work anymore. They appear to click but no action follows. The 2 buttons in question are identical in the inspector to other scenes i havent converted yet. The Vert & UpsideDown Left and Right buttons still work. No issues with the Jump buttons from any direction.
<—Here is image of all the buttons re-enabled before testing.
I have un-enabled the Vert and UpsideDown buttons and the LeftButton and RightButton begin to work properly again. It doesnt appear to be an issue with the code since I can technically fix it by un-enabling the Vert and UpsideDown ones, but I could definitely be wrong. Since the Jump button is always working it seems to assure me it isnt the script.
Images before runtime and after to show they allocate to the proper spots.


Again, I can fix this by manually assigning but I have many levels and would just like to assign at runtime (I do understand I could have assigned everything manually in the amount of time it took to type this lol but what if i change it in the future?). Not having issues assigning buttons as they appear in the correct order and proper locations. Pretty lost here and would love some guidance as I am stuck. Let me know if you would like me to show code but I dont think this is the issue?
Hey Cornysam,
Could you show both the code where you assign the buttons to the button controller, and also the code where you use the buttons? That would make it a bit easier to track down what goes wrong
Sure thing. Sorry it has taken me so long, rough week.
First code snippet is where i assign the buttons (try to at least). This script is on the Player. Note: Everything commented out is what I tried to get working. I commented it out so i could continue being productive on other things.
public class ButtonController : MonoBehaviour {
[SerializeField]
private GameObject[] movementButtons; //set up array for the UI movement buttons
[SerializeField]
private GameObject[] vertMovementButtons; //set up array for the vertical UI movement buttons
[SerializeField]
private GameObject[] upsideDownMovementButtons; //set up array for upsidedown UI movement buttons
GravityRotationChecker gRC; //reference to the GravityRotationChecker script
private void Awake()
{
}
// Use this for initialization
void Start () {
//AssignAllButtons();
gRC = GetComponent<GravityRotationChecker>();
//DisableVerticalButtons();
//DisableUpsideDownButtons();
}
// Update is called once per frame
void Update () {
if (gRC.GravityIsRotating)
{
DisableHorizontalButtons();
EnableVerticalButtons();
}
if(gRC.JumpDirectionUpsideDown)
{
EnableUpsideDownButtons();
DisableVerticalButtons();
}
}
void DisableHorizontalButtons()
{
foreach (GameObject b in movementButtons)
{
b.SetActive(false);
}
}
void DisableVerticalButtons()
{
foreach (GameObject b in vertMovementButtons)
{
b.SetActive(false);
}
}
void DisableUpsideDownButtons()
{
foreach (GameObject u in upsideDownMovementButtons)
{
u.SetActive(false);
}
}
void EnableVerticalButtons()
{
foreach (GameObject b in vertMovementButtons)
{
b.SetActive(true);
}
}
void EnableUpsideDownButtons()
{
foreach (GameObject b in upsideDownMovementButtons)
{
b.SetActive(true);
}
}
void AssignAllButtons()
{
//movementButtons[0] = GameObject.Find("LeftButton");
//movementButtons[1] = GameObject.Find("RightButton");
//movementButtons[2] = GameObject.Find("JumpButton");
vertMovementButtons[0] = GameObject.Find("VertLeftButton");
vertMovementButtons[1] = GameObject.Find("VertRightButton");
vertMovementButtons[2] = GameObject.Find("VertJumpButton");
upsideDownMovementButtons[0] = GameObject.Find("LeftButtonUpsideDown");
upsideDownMovementButtons[1] = GameObject.Find("RightButtonUpsideDown");
upsideDownMovementButtons[2] = GameObject.Find("JumpButtonUpsideDown");
}
}
Here is the code for using the buttons:
namespace UnityStandardAssets.CrossPlatformInput
{
public class AxisTouchButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
// designed to work in a pair with another axis touch button
// (typically with one having -1 and one having 1 axisValues)
public string axisName = "Horizontal"; // The name of the axis
public float axisValue = 1; // The axis that the value has
public float responseSpeed = 3; // The speed at which the axis touch button responds
public float returnToCentreSpeed = 3; // The speed at which the button will return to its centre
AxisTouchButton m_PairedWith; // Which button this one is paired with
CrossPlatformInputManager.VirtualAxis m_Axis; // A reference to the virtual axis as it is in the cross platform input
void OnEnable()
{
if (!CrossPlatformInputManager.AxisExists(axisName))
{
// if the axis doesnt exist create a new one in cross platform input
m_Axis = new CrossPlatformInputManager.VirtualAxis(axisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_Axis);
}
else
{
m_Axis = CrossPlatformInputManager.VirtualAxisReference(axisName);
}
FindPairedButton();
}
void FindPairedButton()
{
// find the other button witch which this button should be paired
// (it should have the same axisName)
var otherAxisButtons = FindObjectsOfType(typeof(AxisTouchButton)) as AxisTouchButton[];
if (otherAxisButtons != null)
{
for (int i = 0; i < otherAxisButtons.Length; i++)
{
if (otherAxisButtons[i].axisName == axisName && otherAxisButtons[i] != this)
{
m_PairedWith = otherAxisButtons[i];
}
}
}
}
void OnDisable()
{
// The object is disabled so remove it from the cross platform input system
m_Axis.Remove();
}
public void OnPointerDown(PointerEventData data)
{
if (m_PairedWith == null)
{
FindPairedButton();
}
// update the axis and record that the button has been pressed this frame
m_Axis.Update(Mathf.MoveTowards(m_Axis.GetValue, axisValue, responseSpeed * Time.deltaTime));
}
public void OnPointerUp(PointerEventData data)
{
m_Axis.Update(Mathf.MoveTowards(m_Axis.GetValue, 0, responseSpeed * Time.deltaTime));
}
}
}
Thanks for the additional code. So, when you are saying that it appears that you successfully click, but no action is then executed, is it OnPointerDown/OnPointerUp that is not executed?
If you are debugging why a click does not trigger an action, one useful tip is to select the UI’s EventSystem while running the scene. You will see the window “EventSystem”, which will show you some more information on what object you are currently hovering over, and clicking on.
Additionally, you could also add a button component to the different UI buttons you have, and see if you can get any UI feedback when interacting with the component.
Let us know how the debugging goes