- Home /
Input System 1.0.0 Sensors - How to get data? [Solved]
I decided to try out the Input System 1.0.0 because you can apparently get sensor input from a phone's sensors.
The quick start guide gives this example code (https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/QuickStartGuide.html):
using UnityEngine;
using UnityEngine.InputSystem;
public class MyPlayerScript : MonoBehaviour
{
void FixedUpdate()
{
var gamepad = Gamepad.current;
if (gamepad == null)
return; // No gamepad connected.
if (gamepad.rightTrigger.wasPressedThisFrame)
{
// 'Use' code here
}
Vector2 move = gamepad.leftStick.ReadValue();
// 'Move' code here
}
}
And then the sensor support page gives this code (https://docs.unity3d.com/Packages/com.unity.inputsystem@1.0/manual/Sensors.html):
InputSystem.EnableDevice(Gyroscope.current);
if (Gyroscope.current.enabled)
Debug.Log("Gyroscope is enabled");
So I tried a basic script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Sensors : MonoBehaviour
{
void Start()
{
InputSystem.EnableDevice(AmbientTemperatureSensor.current);
}
void Update()
{
if (AmbientTemperatureSensor.current.enabled)
{
Debug.Log ("AmbientTemperatureSensor is enabled");
}
}
}
But I get this error
NullReferenceException: Object reference not set to an instance of an object Sensors.Update () (at --- the if (AmbientTemperatureSensor.current.enabled) line)
Based on the fix given by @andrew-lukasik and a comment by @Llama_w_2Ls , I modified my code. Here is the final. I've also posted a screenshot of the screen. As you can see, my particular phone -- a Motorola Android phone -- does not have an ambient temperature sensor but DOES have an accelerometer.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;
public class Sensors : MonoBehaviour
{
public TextMeshPro texttemp;
public TextMeshPro textaccel;
AmbientTemperatureSensor ambient;
Accelerometer accelerometer;
void Start()
{
ambient = AmbientTemperatureSensor.current;
accelerometer = Accelerometer.current;
}
void Update()
{
if (ambient != null)
{
InputSystem.EnableDevice(ambient);
textaccel.text = "enabled";
}
else
{
texttemp.text = "null";
}
if (accelerometer != null)
{
InputSystem.EnableDevice(accelerometer);
textaccel.text = "enabled";
}
else
{
textaccel.text = "null";
}
}
}
var sensor = AmbientTemperatureSensor.current;
if( sensor!=null )
{
/* read values here */
}
else
{
// no sensor connected | no access permission
}
$$anonymous$$ost mobile devices dont have Temperature sensors (in my findings). There are great sensor apps on the playstore to see how each sensor works and if you have it or not.
I think you're right. I picked that sensor kind of arbitrarily. I can try a different sensor, perhaps accelerometer since I know all phones have that. However, I wanted to start with how to basically get data from a phone's built-in sensor.
You'll have to see if the sensor is built into Unity's Input class, for example Input.Gyroscrope
or Input.Accelerometer
etc. If not, there's an asset online called Gyro-Droid, which claims to support all sensors for your mobile device inside of Unity, but it is a bit pricy.
Your answer
![](https://koobas.hobune.stream/wayback/20220613020308im_/https://answers.unity.com/themes/thub/images/avi.jpg)