Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Quick question:
I am writing in unity c#, and have an array containing several instances of the same object. I want to change a non-static bool in every member of the array, and set it to true. How would I do this?
I have played around with for-loops, and searched both here and at the unity forums, but cannot find an answer. I don't believe my messy, faulty code would be of any help. Anybody care to enlighten me? :)
EDIT: Thanks for the tips, will try them now. The code was asked for, excuse the mess:
Component[] toMerge;
for (int t = 0; t < mergeTargets.Length; i++) {
toMerge[t] = mergeTargets[t].gameObject.GetComponent<Enemy>();
toMerge.readyToMerge = true;
}
for (int t = 0; t < toMerge.Length; t++) {
toMerge[t].readyToMerge = true;
}
You have to allocate mergeTargets.Length items in toMerge array and address not the array but its item:
//DONE: We need mergeTargets.Length items in toMerge
Component[] toMerge = new Component[mergeTargets.Length];
for (int t = 0; t < mergeTargets.Length; i++) {
toMerge[t] = mergeTargets[t].gameObject.GetComponent<Enemy>();
//DONE: [t] - we want to change item, not the array
toMerge[t].readyToMerge = true;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
for(int i = 1; i != amount+1; i++)
{
Console.WriteLine("What is the name of the {0} person?", i);
people.Append(Console.ReadLine());
Console.WriteLine("What was their grade?");
grades.Append(Convert.ToDecimal(Console.ReadLine()));
}
(This section is what is affected)
It gets the inputs from the user (I.E. it gets the names and grades, though it doesn't add to the array. I am new to C#, coming from python. Do you guys have any idea how I could fix this issue? TIA
Are you looking for something like this?
using System.Collections.Generic;
int amount = 5;
List<string> people = new List<string>();
List<decimal> grades = new List<decimal>();
for (int i = 0; i < amount; i++)
{
Console.WriteLine("What is the name of the {0} person?", i);
people.Add(Console.ReadLine());
Console.WriteLine("What was their grade?");
grades.Add(Convert.ToDecimal(Console.ReadLine()));
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
hi friends i already finished the multiplication part of this.but i cant insert this "[]" to the numbers like in the above picture anyone can help me to do this using C#.
Without any your code to display data, I can't help you in fact.
So, I suppose, if you convert your numbers into strings or use string.Format (or string interpolation $""), you can add any characters. In a picture I see secondary diagonal matrix.
So, this is example of code, to display data like in a picture
int diag = 9;
for (int i = 1; i < 10; i++)
{
Console.Write($"{i}\t");
for (int j = 2; j < 10; j++)
{
if (diag != j)
Console.Write($"{j * i}\t");
else
Console.Write($"[{j * i}]\t");
}
diag--;
Console.WriteLine();
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Okay, I just need to make sure I'm coding this correctly. Please review the following when you have a moment:
int [] Counter_Event = new int [46];
for (int xCount = 0; xCount < Counter_Event.Length; xCount++)
{ Counter_Event[xCount] = Math.Round(xCount * 10000);}
With the above, it's tossing back a compilation error. I'm probably not using proper syntax, but any perspective would help.
Math.Round() requires a demical or a double as a parameter.
Something like this would work:
int[] Counter_Event = new int[46];
for (int xCount = 0; xCount < Counter_Event.Length; xCount++)
{ Counter_Event[xCount] = (int)Math.Round((double)xCount * 10000); }
You need to typecast the parameter of Math.Round() to either to Double or Decimal. Also, since your array Counter_Event is of type int, so again you have to cast the result of Math.Round() to int as Math.Round() return type is either Decimal or Double.
int [] Counter_Event = new int [46];
for (int xCount = 0; xCount < Counter_Event.Length; xCount++)
{
Counter_Event[xCount] = (int)Math.Round((double)(xCount * 10000));
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
When attempting to use the following bit of code to answer a question Box Blur on the website CodeFights.com, I receive the following message which has me stumped:
file.cs on line 2: error CS0029: Cannot implicitly convert type `int' to `int[][]'
I'm confident that this is something simple that I'm missing but, my searching for an answer to this has not returned any success.
Any ideas as to what simple mistake I am committing?
int[][] boxBlur(int[][] matrix) {
int[][] result = new int[matrix.Length-2][matrix[0].Length-2];
for (int i =1; i < matrix.Length-1; i++)
for (int j = 1; j < matrix[0].Length-1; j++)
{
int sum=0;
for(int k=i-1; k<=i+1; k++)
{
for(int m=j-1;m<=j+1;m++)
{
sum+=matrix[k][m];
}
}
result[i-1][j-1]=sum/9;
}
return result;
}
Wrong call array. Try this(or something like):
int[][] result = new int[matrix.Length - 2][];
for(int i=0; i< matrix.Length - 2; i++)
{
result[i] = new int[matrix[0].Length - 2];
}
int[][] is an array of arrays. You can't initialize the size of the arrays within it when you're initializing it.
If you want a two-dimensional array, use int[,].
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
hi I need to write each element of an array in a line of a textbox
I have tried this code my array is taken from an input file also the number of element of the array
string[] myArray = values[(int)TextBoxIndices.idcourses].Split('-');
string[] tempArray = new string[Int32.Parse(values[(int)TextBoxIndices.totalnbre])];
tempArray = idc.Lines;
for (int t = 0; t < Int32.Parse(values[(int)TextBoxIndices.totalnbre]); t++)
{
tempArray = myArray[t] + '\n';
}
Your for loop is using a value taken from your values array as the count limit, but you are indexing myArray. As you stated in your comment, you are going outside your array bounds.
You should have:
for (int t = 0; t < myArray.Length; t++)