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.
Related
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).
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 2 years ago.
Improve this question
I have a piece of code like this. The problem is that push and pop are not working together. Actually, I think push method working well but there is a problem with pop. Because when the object trigged stack.count increase 1 but when click the button in the canvas ,console print "The Stack is Empty" and stack.count = 0. What is the problem here ?
Stack<Skill> stackSkills;
void Start()
{
stackSkills= new Stack<Skill>();
}
void OnTriggerEnter(Collider collider){
stackSkills.Push(newSkill);
Debug.Log(stackSkills.Count); //When i trigger it , it increases.
}
public void OnClick(){
Debug.Log("clicked");
Debug.Log(stackSkills.Count);
stackSkills.Pop();
}
}
Here is the example:
OUTPUT
1
2
3
clicked
0
InvalidOperationException: Stack empty.
System.Collections.Generic.Stack1[T].ThrowForEmptyStack () (at <ef151b6abb5d474cb2c1cb8906a8b5a4>:0)
The reason why this problem happens is to use this same script in different two Game Object (Canvas and myGameObject) and they are not linked with each other.I am calling OnTriggerEnter() method in myGameObject and OnClick() method in Canvas. Therefore they are not sharing same data fields. There is two Stack in the project.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
So, I'm programming a little word game for a project (with different kinds of didactic games) which basically shuffles the word and the user has to guess it.
Easy so far, but then the same word can't come up more than 1 time so I was using a list to save the used words until I find out the memory problem.
Is there any other way to do it?
My code is this one:
String mixed = "";
while (mixed == "" || mixed == word)
{
mixed = Shuffle(word);
}
for (int i = 0; i < MainWords.used.Count; i++)
{
if (word != MainWords.used[i])
{
MainWords.used.Add(word);
palavraigual = false;
}
}
After 12~15 words the program will run into a out of memory exception. Thanks in advance.
You can be more efficient than a for-loop. This also prevents you from accidentally making the mistake you did.
String mixed = "";
while (mixed == "" || mixed == word)
{
mixed = Shuffle(word);
}
if (!MainWord.used.Contains(word))
{
MainWord.used.Add(word);
palavraigual = false;
}
public static class MainWord
{
public static HashSet<string> used;
}
Instead of the loop, the above utilizes the Contains method. In this case, the HashSet provides a constant-time lookup; however, using Contains rather than a loop to check existence is still good practice even if you using a List or other standard data structure.
Since the above does no looping, it is difficult to accidentally add the word more than once; however, Sets have another beneficial behavior here. They cannot not contain duplicates. If you kept your looping code but changed the data structure to a HashSet you would still avoid your OutOfMemory problem.
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 8 years ago.
Improve this question
I tried this to get the score in my game and I tried this:
foreach (stella stella in stelle)
if (stella.rettangolo.Intersects(giocatore.rect))
{
score=score+10;
}
It functions but not well because if I stay a long time near the star the score continues to inrement even if it is not visible. how can i fix it?
You can add an extra flag that tells you whether or not the star has been picked up. This must be a field in the stella class. Let's call it...
public bool captata; //I'll go with some Google translated Italian.
When the level starts, initialize it with the value false:
foreach (stella stella in stelle)
{
stella.captata = false;
}
When the player intersects the star, you set the flag appropriately. At the same time, you only check for collision if the star hasn't been picked up yet.
foreach (stella stella in stelle)
if ((!stella.captata) && (stella.rettangolo.Intersects(giocatore.rect)))
{
score=score+10;
stella.captata = true;
}
Another way would be to remove the star from the stars collection completely.
for (int i = stelle.Count - 1; i >= 0; --i)
{
if (stelle[i].rettangolo.Intersects(giocatore.rect))
{
score = score + 10;
stelle.RemoveAt(i);
}
}
You need to change your game logic so that you either remove the "star" from the "stars" collection once a player comes near it, or so that you mark it as touched by the player.
Depending on what the logic of the game is, you can also only have the player get points when they start touching the star, but not while they are still touching it. you can have a bool, isTouchingStar and set it to true if you detect the player touching it.
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 combobox, and I would like to search through every element in it.
How can I do this? (also the number of items is not the same everytime, but this is not so important).
I am using c# windows form application.
you can do this
for (int i = 0; i < myComboBox.Items.Count; i++)
{
string value = myComboBox.GetItemText(myComboBox.Items[i]);
}
Use a foreach loop. It will iterate all your items of ComboBox regardless of their count, e.g.
foreach(var item in myComboBox.Items)
{
// do something with your item
}