Hello,
I’m trying to create a game that uses touch input. The panel that the touch inputs are on is loaded when I enter the play scene. I’m trying to set it up so that I can change scenes (to the main menu after exiting play scene), then come back to the play scene. On the first time around, everything loads and functions properly. But when I exit to the main menu, then re enter the play scene, I get the error “There is already a virtual axis named (name of axis) registered.” Here is the code segment that creates the virtual axes:
void CreateVirtualAxes()
{
// set axes to use
m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
// create new axes based on axes to use
if (m_UseX)
{
m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
}
if (m_UseY)
{
m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
}
}
So far I’ve tried checking if the axis already exist before registering them with the following modification to the previous function:
void CreateVirtualAxes()
{
// set axes to use
m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
// create new axes based on axes to use
if (m_UseX)
{
if(!CrossPlatformInputManager.AxisExists(horizontalAxisName))
{
m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
}
}
if (m_UseY)
{
if (!CrossPlatformInputManager.AxisExists(verticalAxisName))
{
m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
}
}
}
but this throws a null reference error saying that m_HorizontalVirtualAxis is not set to an instance of an object.
I’ve also tried removing the virtual axis when changing scenes so that they might be re registered when the play scene is loaded up again but this isn’t working either.
public void RemoveAxes()
{
CrossPlatformInputManager.UnRegisterVirtualAxis(horizontalAxisName);
CrossPlatformInputManager.UnRegisterVirtualAxis(verticalAxisName);
}
Any ideas? Many thanks in advance