There is already a virtual axis named 'x' registered

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 :slight_smile:

Okay so I’ve come up with a solution that appears to be working and isn’t throwing any errors… Though I’m not entirely sure why this works any better than what I tried earlier. Anyway, here’s the code modification:

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))
        {
            CrossPlatformInputManager.UnRegisterVirtualAxis(horizontalAxisName);
        }
        m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
        CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);

    }
    if (m_UseY)
    {
        if (CrossPlatformInputManager.AxisExists(verticalAxisName))
        {
            CrossPlatformInputManager.UnRegisterVirtualAxis(verticalAxisName);
        }
        m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
        CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);

    }
}

Hey,

The error message didn’t seem to make sense. I just copied the steps from a working tutorial. So I closed down Unity and restarted. Problem fixed (again).

Peter

Blockquote
public void RegisterVirtualAxis(CrossPlatformInputManager.VirtualAxis axis)
{
// check if we already have an axis with that name and log and error if we do
if (m_VirtualAxes.ContainsKey(axis.name))
{
//Debug.LogError(“There is already a virtual axis named " + axis.name + " registered.”);
m_VirtualAxes.Remove(axis.name);

            m_VirtualAxes.Add(axis.name, axis);

            if (!axis.matchWithInputManager)
            {
                m_AlwaysUseVirtual.Add(axis.name);
            }
        }
        else
        {
            // add any new axes
            m_VirtualAxes.Add(axis.name, axis);

            // if we dont want to match with the input manager setting then revert to always using virtual
            if (!axis.matchWithInputManager)
            {
                m_AlwaysUseVirtual.Add(axis.name);
            }
        }
    }

Replace this piece of code with yours.

problem there is already virtual axis name vertical registered