- Home /
Command all array variable values
I have a static array variable and i need to give a single command (from another script) to all the values in the array at once.
This is the situation, in Script1.js i have:
static var value : float;
value[0] = 0.1
value[1] = 0.2
value[2] = 0.3
value[3] = 0.4
In Script2.js i need to code this, using a single command (otherwise it won't work):
Script1.value[every value in the array] += 5;
Is it possible? Someone told me to put "id" inside the brackets (Script1.value[id] += 5), but Unity doesn't understand its meaning. I already tried to write:
for (var i : int = 0;i < 3; i++);
Script1.value[i] += 5;
But it doesn't work because all the 4 values become the same. What can i do?
Answer by Bunny83 · Jul 17, 2012 at 11:11 AM
First your variable isn't an array, it's just a float. Second you have to create the array with your desired size. Arrays can't change the size on the fly, if you need something like that, use a generic List.
static var value : float[]; // this is an array
// create the array with 4 elements (index 0 - 3)
value = new float[4];
value[1] = 0.1;
value[2] = 0.2;
value[3] = 0.3;
value[4] = 0.4;
// this will add 5 to each value
for (var i = 0; i < 4; i++)
Script1.value[i] += 5;
Btw, you have a semicolon after the for loop, so the body of the loop is empty. Your code effectively looks like this:
for (var i : int = 0;i < 3; i++)
{
}
Script1.value[i] += 5;
which doesn'T make much sense.
You also didn't even say what exactly you want to do. Try to rephrase your question.
I had already written the correct code, i had forgot to report it here. The variable declaration already was:
static var value : float[];
and before assigning the single values to every array value i had already written
value = new float[4];
but i had forget to insert the brackets in the code! thanks for remembering me to do this! Anyway, i've put the brackets now but the result remains the same: all the 4 array values become a single value.
I'll be more specific: i'm trying to make 4 objects move following 4 different "Ping Pong" trails. But the objects are at different positions in space. The 4 array values are their position on the x axis, generated with 4 different Random.Range(). I want them to start moving at the same time starting from their current position, so in Script1.js they are randomly instantiated in space and in script2.js i would like to use their different x positions to start moving following different trails.
Right now the 4 objects jump to a single x value and then start moving all together.
It seems you've asked that question already two times:
http://answers.unity3d.com/questions/283498/relative-and-absolute-values-problem.html
http://answers.unity3d.com/questions/284058/mathfpingpong-strange-behaviour.html
The for loop you used above certainly doesn't end up with the same value in each variable. When it's executed the values will be 5.1, 5.2, 5.3, 5.4
Is there a reason why you want to handle all those objects form one script? Why not just put this script on each object? Place them where ever you want and they will pingpong between - amplitude and +amplitude around their starting position.
var amplitude = 4.0;
var speed = 1.0;
private var startPos : Vector3;
function Start()
{
startPos = transform.position;
}
function Update()
{
transform.position = startPos + Vector3($$anonymous$$athf.PingPong(Time.time*speed, amplitude*2.0) - amplitude, 0, 0);
}
Your answer
Follow this Question
Related Questions
Script effects all gameobjects. 1 Answer
Static array variable 1 Answer
Change variable of ex2D Sprite using the component ExScreenPosition via code .js script 0 Answers
Storing many array values in a variable 1 Answer
Static variables 1 Answer