Spawn script not working.

Hello i am trying to set random spawning of bot. There is no error in Unity, but nothing happens in game. There is no spawning in game.
I using next code:

using UnityEngine;
using System;
using System.Collections;
using System.IO;

public class Pawns : MonoBehaviour {
public string fileName = "Settings.ini";
float timer = 0.0f;
bool  spawning = false;
public Rigidbody prefab1;
public Rigidbody prefab2;
public Rigidbody prefab3;
public Transform spawn1;
public Transform spawn2;
public Transform spawn3;
public Transform spawn4;
public Transform spawn5;
public Transform spawn6;
public Transform spawn7;
public Transform spawn8;
public Transform spawn9;
public Transform spawn10;
public Transform spawn11;
public Transform spawn12;
public float speed1;
public float speed2;
public float speed3;
private int difficult;
private float rnd1 = 0.5f;
private float rnd2 = 0.3f;
private float rnd3 = 0.2f;

void  Start (){

using (StreamReader fileStream = File.OpenText(Path.GetFullPath(fileName)))
        {
            
            string fileLine = "";
            while ((fileLine = fileStream.ReadLine()) != null)
            {
                string option = fileLine.Substring(0, fileLine.IndexOf("="));
                switch (option)
                {
					case "difficulty":
                        difficult = Convert.ToInt32(fileLine.Substring(fileLine.IndexOf("=") + 1));
                        break;
					case "speed1":
                        timer = Convert.ToSingle(fileLine.Substring(fileLine.IndexOf("=") + 1));
                        break;
					case "speed2":
                        timer = Convert.ToSingle(fileLine.Substring(fileLine.IndexOf("=") + 1));
                        break;
					case "speed3":
                        timer = Convert.ToSingle(fileLine.Substring(fileLine.IndexOf("=") + 1));
                        break;
					
			}

            }

            fileStream.Close();
        }

if (difficult == 1){
rnd1 = 0.5f;
rnd2 = 0.7f;
rnd3 = 0.8f;
}

else if (difficult == 2){
rnd1 = 0.7f;
rnd2 = 0.6f;
rnd3 = 0.7f;
}

else if (difficult == 3){
rnd1 = 0.8f;
rnd2 = 0.7f;
rnd3 = 0.5f;
}

}
void  Update (){ 

 if(!spawning){
  timer += Time.deltaTime;
 } 

 if(timer >= 0.6f){
  Spawn();
 }
}
 
public IEnumerator Spawn (){

 spawning = true;

 timer = 0;
 
 
 if(UnityEngine.Random.value > rnd1) {
 
 	int randomPick1 = Mathf.Abs(UnityEngine.Random.Range(1,5));
 
 	Transform location1;
 	 
 	if(randomPick1 == 1){
	  location1 = spawn1;
	  Rigidbody clone1;
	  clone1 = Instantiate(prefab1, location1.position, location1.rotation) as Rigidbody;
	  clone1.AddForce(new Vector3(UnityEngine.Random.Range(1,4),10,0)*speed1);
	  Debug.Log("Chose pos 1");
	 }
	 if(randomPick1 == 2){
	  location1 = spawn4; 
	  Rigidbody clone2;
	  clone2 = Instantiate(prefab1, location1.position, location1.rotation) as Rigidbody;
	  clone2.AddForce(new Vector3(UnityEngine.Random.Range(1,4),10,0)*speed1);
	  Debug.Log("Chose pos 4");
	 }
	 if(randomPick1 == 3){
	  location1 = spawn9;
	  Rigidbody clone3;
	  clone3 = Instantiate(prefab1, location1.position, location1.rotation) as Rigidbody;
	  clone3.AddForce(new Vector3(UnityEngine.Random.Range(1,4),10,0)*speed1);
	  Debug.Log("Chose pos 9");
	 }
	 if(randomPick1 == 4){
	  location1 = spawn10;
	   Rigidbody clone4;
	  clone4 = Instantiate(prefab1, location1.position, location1.rotation) as Rigidbody;
	  clone4.AddForce(new Vector3(UnityEngine.Random.Range(1,4),10,0)*speed1);
	  Debug.Log("Chose pos 10");
	 }
}
 
 
  if(UnityEngine.Random.value > rnd2) {
 
 	int randomPick2 = Mathf.Abs(UnityEngine.Random.Range(1,3));
 
 	Transform location2;
 	
 	 
 	if(randomPick2 == 1){
	  location2 = spawn2;
	  Rigidbody clone5;
	  clone5 = Instantiate(prefab2, location2.position, location2.rotation) as Rigidbody;
	  clone5.AddForce(new Vector3(UnityEngine.Random.Range(1,4),10,0)*speed2);
	  Debug.Log("Chose pos 2");
	 }
	if(randomPick2 == 2){
	  location2 = spawn8; 
	   Rigidbody clone6;
	  clone6 = Instantiate(prefab2, location2.position, location2.rotation) as Rigidbody;
	  clone6.AddForce(new Vector3(UnityEngine.Random.Range(1,4),10,0)*speed2);
	  Debug.Log("Chose pos 8");
	 }
}
 
 
  if(UnityEngine.Random.value > rnd3) {
 
 	int randomPick3 = Mathf.Abs(UnityEngine.Random.Range(1,3));
 
 	Transform location3;
 	
 	
 	if(randomPick3 == 1){
	  location3 = spawn3;
	  Rigidbody clone7;
	  clone7 = Instantiate(prefab3, location3.position, location3.rotation) as Rigidbody;
	  clone7.AddForce(new Vector3(UnityEngine.Random.Range(1,4),10,0)*speed3); 
	  Debug.Log("Chose pos 3");
	 }
	if(randomPick3 == 2){
	  location3 = spawn7;
	  Rigidbody clone8;
	  clone8 = Instantiate(prefab3, location3.position, location3.rotation) as Rigidbody;
	  clone8.AddForce(new Vector3(UnityEngine.Random.Range(1,4),10,0)*speed3);;
	  Debug.Log("Chose pos 7");
	 }
}
 yield return new WaitForSeconds(0.5f);

 spawning = false;
}
}

Whats the problem?

The actual problem is that you are trying to call a IEnumerable from an update. Not much point in doing that. You could simple make it a normal method, remove the whole yield thing and it should work.

Use InvokeRepeating to clean up the timer stuff in your Update. (remove the update all together for that) and it should start working easily.

That being said. Perhaps a little bit of tinkering could clean this up.

In this code, I pulled all of those randomizing things and pushed it into a single method that you can call with a few variables.

Of course it is completely untested. :wink: But it has no errors in Unity.

using UnityEngine;
using System.Collections;
using System.IO;
 
public class Pawns : MonoBehaviour {
	public string fileName = "Settings.ini";
	
	bool  spawning = false;
	public GameObject easyPrefab;
	public GameObject mediumPrefab;
	public GameObject hardPrefab;
	
	public Transform[] spawnPoints;
	
	public float easySpeed;
	public float mediumSpeed;
	public float hardSpeed;
	private int difficulty;
	float timer = 0.0f;
	private float easy = 0.5f;
	private float medium = 0.3f;
	private float hard = 0.2f;
	
	void  Start (){
		ReadINI();
		
		InvokeRepeating("Spawn", timer, 0.6f);
	}
	
	public void Spawn (){
		if(UnityEngine.Random.value > easy) {
			SpawnPrefab(0,3,easySpeed, easyPrefab);
		}
		
		if(Random.value > medium) {
			SpawnPrefab(4,7,mediumSpeed, mediumPrefab);
		}
		
		if(Random.value > hard) {
			SpawnPrefab(8,9,hardSpeed, hardPrefab);
		}
	}
	
	private void SpawnPrefab(int min, int max, float speed, GameObject prefab){
		int index = Random.Range(min, max);
		if(index >= spawnPoints.Length) return;
		Transform spawn = spawnPoints[index];
	
		GameObject clone = (GameObject)Instantiate(prefab, spawn.position, spawn.rotation);
		clone.rigidbody.AddForce(new Vector3(Random.Range(1,4),10,0) * speed);
		Debug.Log(prefab.ToString() + " instantiated at point: " + spawn.ToString());
	}
	
	private void ReadINI(){
		using (StreamReader fileStream = File.OpenText(Path.GetFullPath(fileName)))
			{
				string fileLine = "";
				while ((fileLine = fileStream.ReadLine()) != null)
				{
					string option = fileLine.Substring(0, fileLine.IndexOf("="));
					string value = fileLine.Substring(fileLine.IndexOf("=") + 1);
					switch (option)
					{
						case "difficultyy":
							difficulty = System.Convert.ToInt32(value);
							break;
						case "speed1":
							timer = System.Convert.ToSingle(value);
							break;
						case "speed2":
							timer = System.Convert.ToSingle(value);
							break;
						case "speed3":
							timer = System.Convert.ToSingle(value);
							break;
					}
				}
				fileStream.Close();
			}
	
		if (difficulty == 1){
			easy = 0.5f;
			medium = 0.7f;
			hard = 0.8f;
		}
		
		else if (difficulty == 2){
			easy = 0.7f;
			medium = 0.6f;
			hard = 0.7f;
		}
		
		else if (difficulty == 3){
			easy = 0.8f;
			medium = 0.7f;
			hard = 0.5f;
		}
	}
}

Thanks for answer, but i solved problem myself, just by removing
yield return new WaitForSeconds(0.5f);
I will try use this code later. Thanks very much.