- Home /
Input System 1.1.0 Resolving ambiguous gyroscope error
Solution: @sacredgeometry pointed out that I needed to write out my reference to the full extent to differentiate the two gyroscopes. While I had tried InputSystem.Gyroscope
vs Gyroscope
thinking the UnityEngine
portion was automatically included, I in fact needed to write it out as UnityEngine.InputSystem.Gyroscope
.
Building on a previous script to see what sensors my phone has, I decided to test for all sensors in the new input management system. Full code at bottom. Everything is set up fine except that I get the error message: Error CS0104 'Gyroscope' is an ambiguous reference between 'UnityEngine.InputSystem.Gyroscope' and 'UnityEngine.Gyroscope'
To use the new Input Management System I need to include a using UnityEngine.InputSystem
statement. However, I also need the using UnityEngine
statement; otherwise, Monobehavior is not recognized. So as far as I know, I need both statements.
I tried to specify in code that the gyroscope I wanted to use was the InputSystem gyroscope by declaring it this way: InputSystem.Gyroscope gyroscope;
However, that leads to Error CS0426The type name 'Gyroscope' does not exist in the type 'InputSystem'
. Does anyone know how to clear up the ambiguity between gyroscopes when using Input Management System 1.1.0? The user guide does not make any statements about confusion between the two. https://docs.unity3d.com/Packages/com.unity.inputsystem@1.1/manual/Sensors.html https://docs.unity3d.com/Packages/com.unity.inputsystem@1.1/manual/QuickStartGuide.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;
public class Sensors : MonoBehaviour {
public TextMeshPro textaccel;
public TextMeshPro textgyro;
public TextMeshPro textgravity;
public TextMeshPro textattitude;
public TextMeshPro textlinearaccel;
public TextMeshPro textmagfield;
public TextMeshPro textlight;
public TextMeshPro textpressure;
public TextMeshPro textproximity;
public TextMeshPro texthumidity;
public TextMeshPro texttemp;
public TextMeshPro textstepcount;
Accelerometer accelerometer;
InputSystem.Gyroscope gyroscope;
GravitySensor gravity;
AttitudeSensor attitude;
LinearAccelerationSensor linearaccel;
MagneticFieldSensor magfield;
LightSensor lightsens;
PressureSensor pressure;
HumiditySensor humidity;
AmbientTemperatureSensor ambient;
StepCounter step;
void Start()
{
accelerometer = Accelerometer.current;
gyroscope = Gyroscope.current;
gravity = GravitySensor.current;
attitude = AttitudeSensor.current;
linearaccel = LinearAccelerationSensor.current;
magfield = MagneticFieldSensor.current;
lightsens = LightSensor.current;
pressure = PressureSensor.current;
humidity = HumiditySensor.current;
ambient = AmbientTemperatureSensor.current;
step = StepCounter.current;
}
void Update()
{
if (accelerometer != null)
{
InputSystem.EnableDevice(accelerometer);
textaccel.text = "enabled";
}
else
{
textaccel.text = "null";
}
if (gyroscope != null)
{
InputSystem.EnableDevice(gyroscope);
textgyro.text = "enabled";
}
else
{
textgyro.text = "null";
}
if (gravity != null)
{
InputSystem.EnableDevice(gravity);
textgravity.text = "enabled";
}
else
{
textgravity.text = "null";
}
if (attitude != null)
{
InputSystem.EnableDevice(attitude);
textattitude.text = "enabled";
}
else
{
textattitude.text = "null";
}
if (linearaccel != null)
{
InputSystem.EnableDevice(linearaccel);
textlinearaccel.text = "enabled";
}
else
{
textlinearaccel.text = "null";
}
if (magfield != null)
{
InputSystem.EnableDevice(magfield);
textmagfield.text = "enabled";
}
else
{
textmagfield.text = "null";
}
if (lightsens != null)
{
InputSystem.EnableDevice(lightsens);
textlight.text = "enabled";
}
else
{
textlight.text = "null";
}
if (pressure != null)
{
InputSystem.EnableDevice(pressure);
textpressure.text = "enabled";
}
else
{
textpressure.text = "null";
}
if (humidity != null)
{
InputSystem.EnableDevice(humidity);
texthumidity.text = "enabled";
}
else
{
texthumidity.text = "null";
}
if (ambient != null)
{
InputSystem.EnableDevice(ambient);
}
else
{
texttemp.text = "null";
}
if (step != null)
{
InputSystem.EnableDevice(step);
textstepcount.text = "enabled";
}
else
{
textstepcount.text = "null";
}
} }
Answer by sacredgeometry · Nov 29, 2020 at 03:58 AM
When you have an ambiguous reference it means that two types exist with the same name the way to get around this is to just specify the type with the full namespace
i.e.
UnityEngine.InputSystem.Gyroscope
You could also use an alias to shorten it: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive
Thanks. That worked. I didn't realize I had to write it ALL the way out. I thought InputSystem.Gyroscope
would do it. I had seen that alias article on $$anonymous$$icrosoft's site but since I didn't know how to write it out, I didn't know how I would do the alias. Now it makes sense.
Wonderful. I don't suppose you $$anonymous$$d accepting my answer please?
I apologize. I upvoted and thought that did it.
Your answer
Follow this Question
Related Questions
How can I get the phone orientation? 3 Answers
Can Unity WebGL access mobile sensors? 1 Answer
Sensor fusion script for ios? 0 Answers
Sensor Fusion - Angular Velocity 0 Answers
Unity5 WebGL gyroscope access 2 Answers