Hi everyone, i’ve implemented this script in my game, but when “seconds” arrive round 38 the game begins to go jerky. I think it’s due to the “for” cicle inside the script. Before this I used another script that implemented only “if” cicles, but it was a 320 lines script and it was too heavy for the game. Can somenone help me replacing this script with another that allows me to do the same things ? Thank you very much.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class StartAll : MonoBehaviour
{
public GameObject[] Terrains = new GameObject[2];
public GameObject[] Stalagts = new GameObject[3];
public GameObject Sphere;
public float timer = 0.0f;
public float seconds;
public float TerrainsY = 5.50f;
public bool Enabled = true;
public int Force_2 = 260;
private int multiplier = 1;
public Canvas MainCanvas;
public Canvas DeathCanvas;
public Canvas PauseCanvas;
public Canvas QuitCanvas;
public Text TimerText;
public GameObject BestImage;
public GameObject BestTimeImage;
public GameObject Points_1;
public GameObject Points_2;
private float[] RandomPositions = new float[15] { 9.25f, 9.15f, 9.00f, 8.75f, 8.50f, 8.25f, 8.00f, 7.80f, 7.60f, 7.30f, 7.10f, 7.00f, 6.75f, 6.50f, 6.25f };
private float[] RandomPositions_1 = new float[15] { 7.45f, 7.20f, 7.05f, 6.85f, 6.65f, 6.35f, 6.20f, 6.07f, 5.93f, 5.75f, 5.57f, 5.40f, 5.23f, 5.10f, 4.90f };
private float[] RandomFlyingPositions = new float[14] { -1.00f, -0.75f, -0.10f, 0.10f, 0.75f, 1.00f, 1.50f, 1.80f, 2.00f, 2.30f, 2.50f, 2.60f, 2.90f, 3.00f };
private float[] RandomFlyingPositions_1 = new float[10] { 1.80f, 2.00f, 2.30f, 2.50f, 2.60f, 2.90f, 3.00f, 3.35f, 3.65f, 3.90f };
void Start()
{
MainCanvas.enabled = false;
DeathCanvas.enabled = false;
PauseCanvas.enabled = false;
QuitCanvas.enabled = false;
BestImage.SetActive(false);
BestTimeImage.SetActive(false);
Points_1.SetActive(false);
Points_2.SetActive(false);
}
void FixedUpdate()
{
if (MainCanvas.enabled == true)
{
if (Enabled)
{
int randFlyPos = Random.Range(0, 13);
int randFlyPos_1 = Random.Range(0, 9);
int randPos = Random.Range(0, 14);
timer += Time.deltaTime * 100.00f;
int sec_int = int.Parse(timer.ToString("f0"));
seconds = sec_int / 100.00f;
TimerText.text = seconds.ToString("f");
if (seconds == 0.2f)
{
GameObject SphereClone = Instantiate(Sphere, new Vector3(-1f, 0f, 0), Quaternion.identity) as GameObject;
SphereClone.transform.SetParent(gameObject.transform);
SphereClone.name = "SphereClone";
SphereClone.GetComponent<Rigidbody2D>().gravityScale = 0.25f;
GameObject TerrainClone = Instantiate(Terrains[0], new Vector3(0.50f, -TerrainsY, 0), Quaternion.identity) as GameObject;
TerrainClone.transform.Rotate(new Vector3(0, 0, 180));
TerrainClone.transform.SetParent(gameObject.transform);
TerrainClone.name = "Ground";
TerrainClone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
GameObject TerrainOppClone = Instantiate(Terrains[0], new Vector3(0.50f, TerrainsY, 0), Quaternion.identity) as GameObject;
TerrainOppClone.transform.SetParent(gameObject.transform);
TerrainOppClone.name = "Ground";
TerrainOppClone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
GameObject TerrainClone1 = Instantiate(Terrains[0], new Vector3(6.40f, -TerrainsY, 0), Quaternion.identity) as GameObject;
TerrainClone1.transform.Rotate(new Vector3(0, 0, 180));
TerrainClone1.transform.SetParent(gameObject.transform);
TerrainClone1.name = "Ground";
TerrainClone1.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
GameObject TerrainOppClone1 = Instantiate(Terrains[0], new Vector3(6.40f, TerrainsY, 0), Quaternion.identity) as GameObject;
TerrainOppClone1.transform.SetParent(gameObject.transform);
TerrainOppClone1.name = "Ground";
TerrainOppClone1.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
}
if (seconds % 1 == 0)
{
if (seconds <= 15)
{
multiplier = 1;
}
else if (seconds > 15 && seconds <= 35)
{
multiplier = 2;
}
else if (seconds > 35 && seconds <= 45)
{
multiplier = 3;
}
else if (seconds > 45 && seconds <= 55)
{
multiplier = 4;
}
else if (seconds > 55)
{
multiplier = 5;
}
for (int i = 0; i < multiplier; i++)
{
if (multiplier == 1)
{
GameObject TriangleClone = Instantiate(Stalagts[0], new Vector3(RandomPositions[randPos], -4.1f, 1), Quaternion.identity) as GameObject;
TriangleClone.transform.SetParent(gameObject.transform);
TriangleClone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
TriangleClone.transform.Rotate(new Vector3(0, 0, 180));
TriangleClone.name = "TriangleClone";
GameObject TriangleOppClone = Instantiate(Stalagts[0], new Vector3(RandomPositions_1[randPos], 4.1f, 1), Quaternion.identity) as GameObject;
TriangleOppClone.transform.SetParent(gameObject.transform);
TriangleOppClone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
TriangleOppClone.name = "TriangleClone";
}
else if (multiplier == 2)
{
GameObject TriangleClone = Instantiate(Stalagts[1], new Vector3(RandomPositions[randPos], -8.0f, 1), Quaternion.identity) as GameObject;
TriangleClone.transform.SetParent(gameObject.transform);
TriangleClone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
TriangleClone.transform.Rotate(new Vector3(0, 0, 180));
TriangleClone.name = "TriangleClone";
GameObject TriangleOppClone = Instantiate(Stalagts[0], new Vector3(RandomPositions_1[randPos], 3f, 1), Quaternion.identity) as GameObject;
TriangleOppClone.transform.SetParent(gameObject.transform);
TriangleOppClone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
TriangleOppClone.name = "TriangleClone";
}
else if (multiplier == 3)
{
GameObject TriangleClone = Instantiate(Stalagts[1], new Vector3(RandomPositions[randPos], -7.20f, 1), Quaternion.identity) as GameObject;
TriangleClone.transform.SetParent(gameObject.transform);
TriangleClone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
TriangleClone.transform.Rotate(new Vector3(0, 0, 180));
TriangleClone.name = "TriangleClone";
GameObject TriangleOppClone = Instantiate(Stalagts[2], new Vector3(RandomPositions_1[randPos], 4.50f, 1), Quaternion.identity) as GameObject;
TriangleOppClone.transform.SetParent(gameObject.transform);
TriangleOppClone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
TriangleOppClone.name = "TriangleClone";
}
else if (multiplier == 4)
{
GameObject TriangleClone = Instantiate(Stalagts[1], new Vector3(RandomFlyingPositions[randFlyPos], -3.75f, 1), Quaternion.identity) as GameObject;
TriangleClone.transform.SetParent(gameObject.transform);
TriangleClone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
TriangleClone.transform.Rotate(new Vector3(0, 0, 180));
TriangleClone.name = "TriangleClone";
GameObject TriangleOppClone = Instantiate(Stalagts[0], new Vector3(RandomFlyingPositions_1[randFlyPos_1], 2.25f, 1), Quaternion.identity) as GameObject;
TriangleOppClone.transform.SetParent(gameObject.transform);
TriangleOppClone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
TriangleOppClone.name = "TriangleClone";
}
else if (multiplier == 5)
{
GameObject TriangleClone = Instantiate(Stalagts[1], new Vector3(RandomPositions[randPos], -7.00f, 1), Quaternion.identity) as GameObject;
TriangleClone.transform.SetParent(gameObject.transform);
TriangleClone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
TriangleClone.transform.Rotate(new Vector3(0, 0, 180));
TriangleClone.name = "TriangleClone";
GameObject TriangleOppClone = Instantiate(Stalagts[2], new Vector3(RandomPositions_1[randPos], 4.50f, 1), Quaternion.identity) as GameObject;
TriangleOppClone.transform.SetParent(gameObject.transform);
TriangleOppClone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
TriangleOppClone.name = "TriangleClone";
}
if (multiplier < 3)
{
GameObject TerrainClone = Instantiate(Terrains[0], new Vector3(8f, -TerrainsY, 0), Quaternion.identity) as GameObject;
TerrainClone.transform.Rotate(new Vector3(0, 0, 180));
TerrainClone.transform.SetParent(gameObject.transform);
TerrainClone.name = "Ground";
TerrainClone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
GameObject TerrainOppClone = Instantiate(Terrains[0], new Vector3(8f, TerrainsY, 0), Quaternion.identity) as GameObject;
TerrainOppClone.transform.SetParent(gameObject.transform);
TerrainOppClone.name = "Ground";
TerrainOppClone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
}
else if (multiplier >= 3)
{
GameObject TerrainClone = Instantiate(Terrains[0], new Vector3(8f, -TerrainsY + 0.50f, 0), Quaternion.identity) as GameObject;
TerrainClone.transform.Rotate(new Vector3(0, 0, 180));
TerrainClone.transform.SetParent(gameObject.transform);
TerrainClone.name = "Ground";
TerrainClone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
GameObject TerrainOppClone = Instantiate(Terrains[0], new Vector3(8f, TerrainsY - 0.50f, 0), Quaternion.identity) as GameObject;
TerrainOppClone.transform.SetParent(gameObject.transform);
TerrainOppClone.name = "Ground";
TerrainOppClone.GetComponent<Rigidbody2D>().AddForce(Vector2.right * -Force_2);
}
}
}
}
}
}
}