For loop does not finish [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I am making a game in Unity, 2D and for the creation of levels, I have added a for loop so that a number of blocks is generated a certain number of times. The problem is that it never ends, that is, instead of ending on the game screen when the two blocks are created, it continues to generate blocks infinitely.
public void GenerateInitialBlocks()
{
for (int i = 0; i < 2; i++)
{
AddLevelBlock();
}
}
I have reset the Script in Unity because it usually gives compilation errors or crashes, but it still doesn't work. Thanks for read.

I believe the problem could be in how many times the method is called.
find where in the code the method is called (perhaps using search)
make sure it's called only once per level and not repeatedly when refreshing or repainting (scene updating methods).

Related

Adding text to a TextBlock, one character at a time [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm posting this so that the code is available to whomever finds it helpful.
Slightly reduced code based on Noctis's suggestion.
StringBuilder DescriptionText = new StringBuilder();
async void RunDescription(StringBuilder description)
{
DescriptionText = description;
await Task.Delay(1000); // Short delay before the text starts printing so the window has time to load
new Thread(AddTextToTextBlock).Start();
}
void TextBlockDispatcher(string text)
{
TextBlock1.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => TextBlock1.Inlines.Add(text)));
}
void AddTextToTextBlock()
{
foreach (char c in DescriptionText.ToString())
{
Thread.Sleep(30);
TextBlockDispatcher(c.ToString());
}
}
Sounds like the usual hoops you need to jump through. The only thing i might consider changing is instead of calling the dispatcher, and then checking if it has access, simply calling it directly on your object.
It should look kinda like :
TextBlock1.Dispatcher.BeginInvoke((Action)(() => /* logic here */ )
Saves you an call, but same same really.
You'll need to massage it in, since this is from the top of my head, but it should point you in the right direction.

Back to the beginning of the code with several events? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm a beginner in C# and I make a quiz. My problem it is that when a question is asked that should make a loop so that the second question can be asked. I had the idea to use goto or return to turn over to the beginning but I have several events of clicks.
Here is an example:
private void Event1
{
//do something..
}
private void Event2
{
//do other things
}
private void Event3
{
//Here I want to return to the event 1 to make a loop
}
Is that possible?
If no are there other solutions?
I assumed that you want to bulid 3 question in series, enable the next options when current question is answered correctly.
If I'm right, first of all you should enable the first button only.
When first event raised and the answer was right , disable first button, then enable next one,so on until all questions were finished.

Counting frequency of characters in a line from a .txt file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a C# task to do and I am stuck on part of the coding. I am using StreamReader to read a .txt file which contains group exam grade data (e.g. ADCBCBBBADEBCCBADBAACDCCBEDCBACCFEABBCBBBCCEAABCBB), I need the code to work out how many A's, B's etc there are inside each set of data, I thought about using some form of count++ code but each attempt just throws errors.
I want it to print onto console the number of A's in that line of the .txt file.
I hope that makes sense, I understand how to do the rest but I just needed a hand on this section.
Consider using System.Linq, eg...
string myString = "ADCBCBBBADEBCCBADBAACDCCBEDCBACCFEABBCBBBCCEAABCBB";
int countOfAs = myString.Count(x => x == 'A');
//Result: 9

Interlocked.Increment at the end of begining of the parallel loop? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Suppose you have a parallel loop in C# and want to use : Interlocked.Increment
Also, suppose there is a heavy process in your loop. Do you think putting Interlocked.Increment before or after that heavy process might make any difference?
In other words, which of the followings is more preferred?
UPDATE: Suppose our criterion for being better or worse is the overall speed.
Prog1:
int iter = 0;
Parallel.For(...{
HeavyProcess()
Interlocked.Increment(iter);
});
Prog2:
int iter = 0;
Parallel.For(...{
Interlocked.Increment(iter);
HeavyProcess()
});
The "one that is preferred" is simply: the one that is functionally correct. Is it meant to record the number of complete operations (end)? or the number of started operations (beginning). Other than that: no difference.

Iterating and instantiating game objects via a list [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a list full of game objects I'm trying to iterate through and spawn one at a time. When a new game object spawns, I'm wanting the old one to disappear. However, at the moment my code is spawning my entire gameobject list as opposed to just the one element.
Can someone please take a look over my code to see what I'm doing wrong?
if(GUI.Button(new Rect(10,130,100,50), "Forward"))
{
for( int i = 0; i < object_List.Count; i++)
{
Instantiate((GameObject)object_List[i]);
}
}
If I understand you correctly, you're trying to instance an object every time you press the button, while at the same time deleting the currently spawned object.
Create a private scoped variable to store your current index:
private int index = 0;
Then you can go ahead and instance on object at a time:
if(GUI.Button(new Rect(10,130,100,50), "Forward"))
{
if (index > 0 && object_List[index] != null)
{
Destroy((GameObject)object_List[index]);
}
Instantiate((GameObject)object_List[index]);
index ++;
}
You just need to do some checking whether index is actually larger than your list, so you won't get an IndexOutOfBounds Exception.

Categories

Resources