Script replacing suggestions.

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);
                        }
                    }
                }
            }
        }
    }
}

OK, I see a number of general issues in your script. I’m not sure if any of them are your problem, but all of them are certainly contributing to your script being tough to follow, which makes it tough to track down your problem.

  1. First and foremost, learn to use functions - if you find yourself copying and pasting a bunch of code over and over you’re probably wasting a lot of work.

In this case, you should have a function probably something like:
(This is a rough function, you might have to add a few parameters to it to make it work. I didn’t analyze your code really deeply)

public void GenerateTriangles(GameObject triangle, GameObject oppTriangle) {
GameObject TriangleClone = Instantiate(triangle, 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(oppTriangle, 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";
}

(It’s harder to analyze the code at the beginning but this could probably be used there too.)

This will turn your hundreds-of-lines if statement into something you can clearly see on one screen.

  1. The way you handle seconds is asking for trouble. Comparing floats using == is always chancy. In this case, it’s probably working well enough because it’s in FixedUpdate and your fixedDeltaTime is 0.02, but even then comparing floats to each other using == is risky because floats have an inherent inaccuracy. (1f + 1f) / 2f may return 0.999999f or 1.0000001f - that’s always going to be something that can happen with floats. Besides that, what are you going to do if you want to change your time interval to something that isn’t perfectly divisible by 0.02, or when you change your fixedDeltaTime down the road to fix some physics bug?

Instead of checking to see if the time is divisible by something in particular, instead, track the last time you did a thing, and then check to see if Time.time >= lastTimeYouDidAThing + timeInterval. It’ll work more reliably, your intentions will be clearer to anyone reading it, and it’s portable outside of FixedUpdate().

  1. if you have a long chain of “if … else” and what they’re determining is pretty simple, I find it makes code a lot more readable to compress them down to single, repetitive lines. Also, since you’re using “else if”, half of your comparisons are redundant, which can be an annoyance in maintaining your code. (It’d be easy to change one of your time cut-offs later and introduce a new bug by forgetting to change the second time you check against it, for example.)
if (seconds <= 15f) multiplier = 1;
else if (seconds <=35f) multiplier = 2;
else if (seconds <= 45f) multiplier = 3;
else if (seconds <= 55f) multiplier = 4;
else multiplier = 5;

Now, I have a suspicion as to what is actually causing your jerkiness problem - I bet that you are instantiating a lot more objects than you think you are. It appears that, once a second, you generate at least 2 new objects; after 15 seconds, you’ll generate 4 new objects every second; after 55 seconds, you’re generating 10 objects every second. You say you start hitting performance issues around 38 seconds, when by my math you’ll have around 146 new objects in the scene - if each of those has a renderer and/or a rigidbody/collider, then that is an awful lot of objects and would certainly explain the slowdown.

Hi @StarManta , sorry for the late answer.
Well, thank you very much for your tips, I’ll try to fix this problem by following your tips. If I manage to fix them, I’ll teel you.:slight_smile: