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?