- Home /
C#: Array question, check value
Hi! I got a few floats and I want to be able to check when 1 of them has set a value to it. Like when x1 = 20f; I want to be able to check it. And if so, how to remove that value. I'm not sure how to do this. I thought an array or something but I'm not sure how to check if one has a value.
The floats: v
private float x1;
private float x2;
private float y1;
private float z1;
private float z2;
private float z3;
Answer by mattyman174 · Mar 20, 2014 at 10:06 AM
private float[] floatArray = new float[6] {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f}; // Define a new float array and give it the name 'floatArray', fill it with some values.
public float x1;
x1 = floatArray[0]; // Assign the value in the first index of our floatArray to the variable x1;
// x1 will now equal 1.0f
Alternatively instead of assigning values to the Array in its definition, you can assign values to its indices 1 at a time like so.
private float[] floatArray = new float[6]; // Define the array.
floatArray[0] = 1.0f; // Assign values one at a time to each index.
floatArray[1] = 2.0f;
floatArray[2] = 3.0f;
floatArray[3] = 4.0f;
floatArray[4] = 5.0f;
floatArray[5] = 6.0f;
Keep in mind that Arrays are of fixed length. The 0th index is always the first index in the array.
Yes thanks for your help. $$anonymous$$y goal is that only one of the floats can have a value for example 20, and the rest is put to 0. Hope im making my case clear here :)
Just assign 0 to all other floats except the one you want to be different?
Is there a condition in which all other floats are to remain 0 or is this just your preference for the moment?
From the limited information you have given i cannot provide a sufficient answer if your having other difficulties.
Assigning values to your array indicies is as simple as i have outlined above, if you want only a single float to be a value other than 0 refer to below.
private float[] floatArray = new float[6]; // Define the array.
floatArray[0] = 20.0f; // Assign values one at a time to each index.
floatArray[1] = 0f;
floatArray[2] = 0f;
floatArray[3] = 0f;
floatArray[4] = 0f;
floatArray[5] = 0f;
@ChrisJoosten: No, it's not really clear what you actually want to do. If only 1 of them is used at a time, why do you actually need the others? A single float variable and an additional int variable would serve the same purpose:
float theValue = 20.0f;
int theIndex = 5;
so the 5. "value" is set to 20
It has to be dynamic.
Im using them to change the rotation from an object. so when i set the value of x1 to 20, the objects rotates/moves to that direction. That's why I only want one float at a time to have a value else the object will be flying randomly through the scene :p
There is specific functions designed to help you Rotate Objects.
http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html