- Home /
Why doesent my code get executed?
Hello, I have a problem whith getting my code to work properly. Console is error free, but part of my code (ll.109-151) does not get executed and Iam not able to find the cause of that. I testet it and everything until line 109 works as supposed. Does anyone of you guys have an idea what might go wrong there? Thank you for your time and help.
Answer by Bunny83 · Aug 24, 2017 at 12:57 PM
GeneratePlanet is a coroutine. Coroutines are not methods but statemachine generators. When you call "GeneratePlanet();" you just create a statemachine which is returned by "GeneratePlanet" but you don't do anything with it. To actually run a coroutine you have to use StartCoroutine and pass the statemachine as argument:
StartCoroutine(GeneratePlanet());
Also, that for loop should look more like this if you want it to run "SpawnNumber" times.:
for(int i = 0; i < SpawnNumber; i++)
You can read more about for loops here
You have some other issues in your code, as well as much potention for improvement.
your line if(Pos == 50)
will never execute because the max value of Random.Range with integer parameters is exclusive. So it can only return values in the range of "0-49".
When you want to check a range you can simply do
if (Pos >=10 && Pos <=19)
Or in case of a chain like yours you can simply use else if
like this:
if (Pos < 10)
{
}
else if(Pos < 20)
{
}
else if(Pos < 30)
{
}
// ...
Though it would be much better to use an Array of Transforms for your spawn points and just use the random value as index into that array.
You should never use "==" in a for loop. Your loop will only run when "SpawnNumber" is exactly "0". Any other value would skip the whole loop. The loop runs as long as the condition is true. Your condition is only true when i == SpawnNumber. Since i starts at 0 SpawnNumber has to be 0 as well. So the whole loop makes no sense. It's just the same as doing:
if (SpawnNumber == 0)
You probably want this:
for(int i = 0; i < SpawnNumber; i++)
This will iterate the loop "SpawnNumber" times.
(ps Wenn du Probleme hast meine Antwort zu verstehen, sag' bescheid -.-. Otherwise stick to english).
Your answer
Follow this Question
Related Questions
Save GameObject to file without Playerpref 0 Answers
Save GameObject to file without Playerpref 2 Answers
How can i make the camera to lookat the next target and then rotate and move to the next target ? 0 Answers
How can i use the object pool script to create and destroy gameobjects ? 0 Answers
How can I get the SerializedObject and FineProperty also the node tree childs of conversations ? 0 Answers