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);
		};

Ahh correct, sorry my bad, see the correction :)

2 Answers

2

@benpaddlejones

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 ^^.

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);
}

Do you mean [this][1] property? I think it will not work due that RectTransform.rect is just a calculated value and not a actual rect, so you can't change it and even if you can do it - it will not affect? ? [1]: http://docs.unity3d.com/ScriptReference/RectTransform-rect.html