- Home /
SystemInfo.deviceType == DeviceType.Handheld not working? (Unity 5)
Hi I'm trying to have different controls depending on device type. Below is my code but for some reason it's not working? I get an error that "movement cannot be used before it is declared". The controls work perfectly standalone so the error is in my if, but not sure where (new to C#).
Any help appreciated?
Ben
if (SystemInfo.deviceType == DeviceType.Handheld)
{
Vector3 movement = new Vector3 (Input.acceleration.x, 0.0f, Input.acceleration.y);
} else {
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
};
Answer by Ascidion · Dec 19, 2015 at 06:02 AM
You could try to declare your movement variable outside the If statement and then assigning the new Vector3 inside the If statement.
Vector3 movement;
if (SystemInfo.deviceType == DeviceType.Handheld)
{
movement = new Vector3 (Input.acceleration.x, 0.0f, Input.acceleration.y);
}
else {
movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
};
Just realised that this is an old question and got bumped because you posted an answer which however was stuck in the moderation queue ^^.
Answer by Bunny83 · Dec 19, 2015 at 05:59 AM
You declare your movement Vector3 variable inside the if body. The variable is only valid inside that block. You have to declare it outside of your if statement:
Vector3 movement;
if (SystemInfo.deviceType == DeviceType.Handheld)
{
movement = new Vector3 (Input.acceleration.x, 0.0f, Input.acceleration.y);
} else {
movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
}