i want to make many lines use line renderer and i need double array. One of them is number of lines, and other one is data of points position.
...
positions[n + 1] = new Vector3(positions[n].x + Bxpe, positions[n].y + Bype, positions[n].z + Bzpe);
linerenderer.SetPositions(positions);
...
it work when number of line is one but
...
positions[z,n + 1] = new Vector3(positions[n].x + Bxpe, positions[n].y + Bype, positions[n].z + Bzpe);
linerenderer[1].SetPositions(positions[???]);
...
when i want to make like this, i dont know how write it right way.
pleace help
If you really want to keep using a 2d-array checkout how to "slice" arrays out of it
However, in your case instead of a 2-dimensional array
Vector3[,]
I would rather simply use a jagged array
Vector3[][]
and then do e.g.
positions[z][n + 1] = positions[z][n] + new Vector3(Bxpe, Bype, Bzpe);
...
linerenderer[z].SetPositions(positions[z]);
Related
This if statement within the update() have 2 for-loop, but it only runs the first one after the if condition is activated, and I don't know why.
I'm building a code for path optimizing in unity. Currently I have to find out the path that came across the nodes/points/positions with a certain positions array that the index is the order the path should follow. Some path between 2 nodes are repeated , ex: A to B and B to A is consider the same path and shall thicken the width of line AB eventually rendered. So I tried to sort out the position array into 2 different array for comparing if any of the pair of nodes(or we can say line) is repeated. And I encountered a problem in if statement within the update().
The first should sort out the original array for later comparison. The second one is just for testing if the first one do their job. No comparing yet. However after hitting play and satisfy the if statement I can see all the Debug.log in the first one, everything is normal, the sorting is normal, while the second one just doesn't print anything at all.
I tried comment out the first one, and the second one will run.
I tried to put second one outside the if statement, after it, and without commenting the first one, the second one won't run.
I tried to put the second one before the first one, in the if statement, the second one will run and the first one won't.
So I think this might be some kind of syntax error or am I using the if statement wrong? Please help.
if (l > 0)//activate when we choose any preset processes
{
for (int n = 0; n <= positions.Length; n++)//this loop will sort all the pos1 and pos 2 into array for current frame
{
curPos_1 = positions[n];//current position of node 1
curPos_2 = positions[n + 1];
Debug.Log("CURPOS_1 of line number " + n + " is " + curPos_1);
Debug.Log("CURPOS_2 of line number " + n + " is " + curPos_2);
flag[n] = 0;
Pos_1[n] = curPos_1;
Pos_2[n] = curPos_2;
Debug.Log("POS_1 array of line number " + n + " is " + Pos_1[n]);
Debug.Log("POS_2 array of line number " + n + " is " + Pos_2[n]);
}
for (int o = 0; o <= positions.Length; o++)
{
Debug.Log("flag of number " + o + " is " + flag[o]);
}
}
As described, all for loop should print something. Not just one of it.
Have you checked your Unity Console Window ?
In your first loop you get the next item but its condition will fail at the end, i.e. off by one.
Correct code should be something like this:
var floats = new float[100];
for (var i = 0; i < floats.Length - 1; i++)
{
var f1 = floats[i];
var f2 = floats[i + 1];
}
Now, Unity, has a behavior of ON ERROR RESUME NEXT, so it's highly probable that an error has occured but you haven't seen it (did you turn off the red icon for toggling errors in console ?).
Also, for some conditions only you know about (you didn't post the whole context), it could work once after you've changed some state of your program.
firstRound = (i + "&" + partnerOne);
secondRound = (i + "&" + partnerTwo);
while (firstRound.Equals(secondRound))
{
myCount = myCount + 1;
spotTwo = rnd.Next(heelerEntries.Count);
partnerTwo = heelerEntriesTwo[spotTwo];
secondRound = (i + "&" + partnerTwo);
if (myCount == 5)
{
MessageBox.Show("Messed up 5 times!");
break;
}
My problem is that sometimes when executing the code, i will end up with the same team "A&G" for round one, and "A&G" for round two. List 1 has ABCDEF and list 2 has GHIJKL. I want it to check the teams from both rounds, and if any are the same, redo the randomizing for the second round.
As seen in the code above, i am trying to see if the strings firstRound and secondRound are the same. If they are, i want it to redo the entire second round loop. The code is for randomizing teams from a list so i cannot have the same team for the different rounds. If anyone is able to help, or give me other ideas on ways to do this, i appreciate it.
I'm working on a game in Unity, and just noticed an odd bug that comes up sometimes. What's happening is that a string array that I'm instantiating is being created too short, even though the log messages I'm writing confirm that the expression should be creating a larger array.
Here's the relevant snippet of code:
Debug.Log("hero.power = " + hero.power.ToString());
Debug.Log("allyPower = " + allyPower.ToString());
Debug.Log("opponent.power = " + opponent.power.ToString());
int max = Mathf.Max(hero.power + allyPower, opponent.power);
report.flips[hero.name] = new string[hero.power + allyPower];
report.flips[opponent.name] = new string[opponent.power];
Debug.Log("max = " + max.ToString());
Debug.Log("report.flips[hero.name].Length = " + report.flips[hero.name].Length.ToString());
And the output this creates in Unity:
This shows that hero.power is 3, and allyPower is 0 but instead of creating a string[3] it creates a string[1].
Is there something about using expressions to determine the length of arrays that's causing an issue here? I temporarily changed to this:
int heroArrayLength = hero.power + allyPower;
report.flips[hero.name] = new string[heroArrayLength];
And it seems to have helped, though the bug was erratic before so I'm not entirely certain it's fixed. And even if it is, I'm still unclear what the actual cause is.
Does anyone know what's happening here?
If hero.name is the same as opponent.name, then:
report.flips[hero.name] = new string[hero.power + allyPower];
report.flips[opponent.name] = new string[opponent.power];
will both be setting the same "flips" (whatever "flips" is/are), and the second time "wins". Since opponent.power is 1, the final array will be of length 1, which means that report.flips[hero.name] will be length 1.
I had three values (Name, Course and Average) which were assigned to 3 arrays. I had to combine them and put them in a listbox. Now, I need to be able to select the same line and break it back up into the 3 variables.
My listbox output looks like this:
Lastname, firstname SEC360 93.5
I tried to do a split with space, but that breaks up my lastname and firstname, which need to be one combined variable with the comma included (I need to check it against the array in which it is placed). I cannot do substring either, because I do not have a set value. Any ideas?
EDIT:
I am sorry everyone. Im a inexperienced programmer (to say the least) and new to this site.
This is where I loaded the arrays:
studentNamesAr[studentCount] = studentNameTxtBox.Text;
courseAr[studentCount] = courseNumTxtBox.Text;
gradesAr[studentCount, 0] = Convert.ToInt32(grade1TxtBox.Text);
gradesAr[studentCount, 1] = Convert.ToInt32(grade2TxtBox.Text);
gradesAr[studentCount, 2] = Convert.ToInt32(grade3TxtBox.Text);
gradesAr[studentCount, 3] = Convert.ToInt32(grade4TxtBox.Text);
This is where I load the arrays to the listbox:
for (int i = 0; i != studentCount; i++)
{
studentAvg = ((gradesAr[i, 0] + gradesAr[i, 1] + gradesAr[i, 2] + gradesAr[i, 3]) / 4);
studentListBox.Items.Add(string.Format("{0, -20} {1, 20} {2, 20:F1}", studentNamesAr[i], courseAr[i], studentAvg));
}
Yes this is Windows Forms.
Data is not lost in arrays. When the program runs I should have maybe 5 entries. I need to split them back up so that when I select one from the listbox (to delete it), I will find the values in the 3 arrays and delete them, and then shift the remaining array values up.
If you preserve the order of your items in the listbox, the SelectedIndex of the listbox should match up with the indices from your original arrays. So once you have that, you can go about shifting your arrays up (which is pretty tedious).
For the record, there are much better constructs to use to approach this problem, but I'm assuming the stipulation of using arrays is part of a homework assignment. :)
You could hold the value separated by some other delimiter in the .Tag property.
I suggest you to replace the ", " (notice the space after the ',') with something else (like ";") then use the split method with space as the parameter.
string s = "Lastname, firstname SEC360 93.5";
string[] result = s.Replace(", ", ";").Split(' ');
then you may change back the result[0] by replacing ";" with ", "
I am working on a text-based game, and I want to print the results in the end.
However, at the moment it only prints the latest input data and not the 5 loops in the array.
This is my array
int[] turnarr = new int[5];
turnarr[x] = turn;
for (int i = 0; i < turnarr.Length; i++)
Console.WriteLine(turnarr[i] + "\t" );
It's hard to be certain, as I only see part of the code, but I suspect that you are recreating the turnarr array in each turn, which would make every entry except the last one zero.
If the value of x never changes then you're only writing to a single item in the array, and thus overwriting it every time with the latest value of turn.
If turn is your last turn value, and x is 4, you will see four zeroes on their own lines and then the value of turn because you are only assigning to the xth index of turnarr
I took a look at your pastebin and tracked the issue down I believe:
The following line:
Console.WriteLine(turnarr[i] + "\t" + windarr[i] + " ms \t" + apmeterarr[x] + "m\t\t" + lenghtarr[x] + " meter\t\t");
you are using i for 2 spots, and x for the other 2 for your index variable...
Change
apmeterarr[x] and
lenghtarr[x]
To
apmeterarr[i] and
lenghtarr[i]