New to scripting, having trouble with a "switch"

I am really really new to scripting and this week we are without guidance on how to create a speedometer for a vehicle. As resources we are given 25 .png’s of a speedometer going from 0-120. I thought if I used a GUI texture I could use a switch function, except now my lack of experience with scripting has caught up to me. I have this much on the script so far (I tried to cheese the switch using the input instead of a direct correlation to the speed of a ridgidbody)

var imgSpeedo_00 : Texture2D;
var imgSpeedo_01 : Texture2D;
var imgSpeedo_02 : Texture2D;
var imgSpeedo_03 : Texture2D;
var imgSpeedo_04 : Texture2D;
var imgSpeedo_05 : Texture2D;
var imgSpeedo_06 : Texture2D;
var imgSpeedo_07 : Texture2D;
var imgSpeedo_08 : Texture2D;
var imgSpeedo_09 : Texture2D;
var imgSpeedo_10 : Texture2D;
var imgSpeedo_11 : Texture2D;
var imgSpeedo_12 : Texture2D;
var imgSpeedo_13 : Texture2D;
var imgSpeedo_14 : Texture2D;
var imgSpeedo_15 : Texture2D;
var imgSpeedo_16 : Texture2D;
var imgSpeedo_17 : Texture2D;
var imgSpeedo_18 : Texture2D;
var imgSpeedo_19 : Texture2D;
var imgSpeedo_20 : Texture2D;
var imgSpeedo_21 : Texture2D;
var imgSpeedo_22 : Texture2D;
var imgSpeedo_23 : Texture2D;
var imgSpeedo_24 : Texture2D;
var imgSpeedo_25 : Texture2D;

function Update () {
switch(Input.GetKeyDown(KeyCode.UpArrow))
// this is where I begin to get lost, Im not sure if i need an external script to interact with in order to make the switch work
{
case 0:
guiTexture.texture = imgSpeedo_00;
break;

case 1:
guiTexture.texture = imgSpeedo_01;
break;

case 2:
guiTexture.texture = imgSpeedo_02;
break;

case 3:
guiTexture.texture = imgSpeedo_03;
break;

//ETC…
}
}

would this be able to work with some tweaking, or should I rethink on how to change the texture of the GUI as my ridgidbody’s speed increases. If I do need to use the speed in order to make the speedometer work, would I have to create a static variable for that? like

static var speed : float = 0;

?
I guess what I am getting at is that I need help with basic scripting that I can’t really get from my teacher (very hands off instructor)

anything you guys can help with is awesome, any resources that can walk me through and hold my hand while it does so would be awesome.

this could work but you need more then just a switch statement in update:
switch(Input.GetKeyDown(KeyCode.UpArrow))
each case is based on what Input.GetKeyDown(KeyCode.UpArrow) returns so case 0, case 1, case 2 etc make no sense as Input.GetKeyDown(KeyCode.UpArrow) returns either true or false not 0,1,2,3 instead you can have a counter in your update like you asked about

you want your speed var to change over time so you can do
switch(speed) and then a case for each different value,
case 1 when speed = 1
etc.

Ok, so I changed it around a little bit,

function Update () {
switch(speed)
{
case 0: (speed = 0)
guiTexture.texture = imgSpeedo_00;
break;

case 1: (speed = 0.05)
guiTexture.texture = imgSpeedo_01;
break;

case 2: (speed = 0.1)
guiTexture.texture = imgSpeedo_02;
break;

case 3: (speed = .15)
guiTexture.texture = imgSpeedo_03;
break;

But I am getting syntax errors. I apologize for how base level I am. But I dont know how to code the change of my speed to correspond with the cases. My best guess was to add the (speed = ??) after the colon, do I need to ad an “if” statement prior to it?

Cases are based off what you are switching, so instead of 1,2,3… it should be the actual value you are testing

function Update () {
	switch(speed)
	{
	case 0: 
		guiTexture.texture = imgSpeedo_00;
		break;
		
	case 0.05:
		guiTexture.texture = imgSpeedo_01;
		break;
		
	case 0.1:
		guiTexture.texture = imgSpeedo_02;
		break;
		
	case  0.15:
		guiTexture.texture = imgSpeedo_03;
		break;

There you go, that is how switches work
Hope that helps
Myhi

Okay… So i have the switch working, no compiler errors. How do I get it to pick up the speed of my ridgid body, i thought that if I made the variable “speed” static that I would be able to access it for the switch in this script.

static var speed : float = 0;

var imgSpeedo_00 : Texture2D;
var imgSpeedo_01 : Texture2D;
var imgSpeedo_02 : Texture2D;
var imgSpeedo_03 : Texture2D;
var imgSpeedo_04 : Texture2D;
var imgSpeedo_05 : Texture2D;
var imgSpeedo_06 : Texture2D;
var imgSpeedo_07 : Texture2D;
var imgSpeedo_08 : Texture2D;
var imgSpeedo_09 : Texture2D;
var imgSpeedo_10 : Texture2D;
var imgSpeedo_11 : Texture2D;
var imgSpeedo_12 : Texture2D;
var imgSpeedo_13 : Texture2D;
var imgSpeedo_14 : Texture2D;
var imgSpeedo_15 : Texture2D;
var imgSpeedo_16 : Texture2D;
var imgSpeedo_17 : Texture2D;
var imgSpeedo_18 : Texture2D;
var imgSpeedo_19 : Texture2D;
var imgSpeedo_20 : Texture2D;
var imgSpeedo_21 : Texture2D;
var imgSpeedo_22 : Texture2D;
var imgSpeedo_23 : Texture2D;
var imgSpeedo_24 : Texture2D;

function Update () {
switch(speed)
{
case 0.00:
guiTexture.texture = imgSpeedo_00;
break;

case 0.05:
guiTexture.texture = imgSpeedo_01;
break;

case 0.1:
guiTexture.texture = imgSpeedo_02;
break;

case 0.15:
guiTexture.texture = imgSpeedo_03;
break;

case 0.2:
guiTexture.texture = imgSpeedo_04;
break;

case 0.25:
guiTexture.texture = imgSpeedo_05;
break;

case 0.3:
guiTexture.texture = imgSpeedo_06;
break;

case 0.35:
guiTexture.texture = imgSpeedo_07;
break;

case 0.4:
guiTexture.texture = imgSpeedo_08;
break;

case 0.45:
guiTexture.texture = imgSpeedo_09;
break;

case 0.5:
guiTexture.texture = imgSpeedo_10;
break;

case 0.55:
guiTexture.texture = imgSpeedo_11;
break;

case 0.6:
guiTexture.texture = imgSpeedo_12;
break;

case 0.65:
guiTexture.texture = imgSpeedo_13;
break;

case 0.7:
guiTexture.texture = imgSpeedo_14;
break;

case 0.75:
guiTexture.texture = imgSpeedo_15;
break;

case 0.8:
guiTexture.texture = imgSpeedo_16;
break;

case 0.85:
guiTexture.texture = imgSpeedo_17;
break;

case 0.9:
guiTexture.texture = imgSpeedo_18;
break;

case 0.95:
guiTexture.texture = imgSpeedo_19;
break;

case 1.0:
guiTexture.texture = imgSpeedo_20;
break;

case 1.05:
guiTexture.texture = imgSpeedo_21;
break;

case 1.1:
guiTexture.texture = imgSpeedo_22;
break;

case 1.15:
guiTexture.texture = imgSpeedo_23;
break;

case 1.2:
guiTexture.texture = imgSpeedo_24;
break;

}

}

So this is the script I have after the help. No I don’t know how to connect it with the speed of my ridgidbody. I don’t know if i need to attach a script to the instrument panel (this one) as well as attach another script to the ridgidbody in order to track its speed.

You should never use floats in a switch. The likelihood of that ever working correctly are slim to none.

You are making some kind of speedo correct… can you explain the purpose of those 24 images? Regardless of the type of speedo you are making, that setup seems completely redundant and poorly architectured.

I would have two textures.

one for the speedo itself
one for the needle. The needle would be rotated depending on the speed.

We are given 24 images that are from 0-120 mph and we need to make a speedometer that functions using them. I wish I could use a needle and face instead of the 24 images, however that isnt the assignment. So instead of a flat, would I slim it down to “static var speed”?

I will use c#. The translation to UnityScript shouldn’t be too complex for you.

Instead of declaring 24 texture variables, that is what you would use an array or a list for.

List<Texture2D> speedoImages;

Instead of a huge, 24 cases long switch, try to rethink how can you make it shorter. Whenever you see a lot of repetitive code, a bell should ring in your head warning that you are doing something wrong.
The minimum speed is 0, and the maximum will be 120. We will treat all speed values above 120 as 120 because your speedometer cannot measure higher than that.

void Update()
{
    var speed = rigidbody.speed;
    var maxIndex = speedoImages.Count-1;
    var speedIndex = Mathf.Round(speed*maxIndex/120f);
    var index = Mathf.Min(maxIndex, Mathf.Max(0, speedIndex));
    guiTexture = speedoImages[index];
}

To select which index in your list of images is the right one for the current speed, we use a rule of three (assuming that your images are uniformly covering all the speeds from 0 to 120).

if 120 would correspond to maxIndex 
then to current speed would correspond  x

We get speedIndex as the rounded value from that x, because indices must always be integers and that x probably gives us a float with decimals.
Finally, we make sure the speedIndex we got is within the minimum and maximum limits.

so… ninety percent of what was said went over my head. I understood the grouping of the images under a set as an index. However I dont know most of the commands you wrote. Im in my first semester of understanding scripting. While there are much more efficient ways for me to do this, what I need more is to understand how to do this on its most basic level.

Im sorry that what I seems to be an elegant solution to my dilemma goes right over my head but im at level 0 right now.

I used a List and 3 math functions that return a rounded up value, the maximum between two values, and the minimum between two values, respectively.
Explanations for them are in the documentation, although the math I used there is at elementary school level.
In my experience, 90% of any programming is thinking on the problem, reading the documentation, and remembering the math from the school. You just don’t solve a problem by throwing lines and lines of code at it until it works (as your first solution was), and even if it works, it will be a mess to deal with.

If you absolutely don’t want to use a list or an array, or the most elementary math to map your continuous range of speeds from 0…120 km/h to your discrete range of image indices of 0…24, then I suggest to use series of if blocks that test whether the rigidbody.speed is between 0 and 5, then image0 is selected, else if it is between 5 and 10 then image1 is selected, etc.

But it is a hack. :slight_smile: