2
\$\begingroup\$

Games Basic Premise

Ball rolls along procedurally generated path. Click to changer directions. Don't fall off. I have a script dedicated to the path. The path is made up of 20 cubes, along the x+ and z+ axis. Path is generated at the start then changes randomly as you go.

Problem

In my path designer script, I also control another cube I call "Fake Path". It's a single cube to throw off the player. But whenever the control script runs it freezes my game, I'm assuming it's processing something. Then after awhile it might resume the game. I know my code is probably a little wonky since I'm still getting the basics of game design. Key_CircLinkList is a circular linked list. With each element storing an index.

void Start(){
    sphere = GameObject.Find("Sphere");
    sphereScript = sphere.GetComponent<SphereMove>();
    startingCube = GameObject.Find("StartCube");
    fakeCube = GameObject.Find("FakeCube");
    randIndex = 10;
    isGenerated = true;

    pathV2 = new Key_CircLinkList<GameObject>();
    pathV2.AddFirst(startingCube);

    for (int i = 0; i < pathLength-1; i++)
    {
        float random = Random.value;
        pathV2.AddLast(Instantiate(startingCube, this.transform));
        if (random <= 0.5f)
        {
            pathV2.GetLast().GetElement().transform.position = new Vector3(pathV2.SelectInd(i).GetElement().transform.position.x + distMove, pathV2.SelectInd(i).GetElement().transform.position.y, pathV2.SelectInd(i).GetElement().transform.position.z);
            //Setting initial movement direction
            if (i == 0)
            {
                sphereScript.l_or_r = true;
            }
        }
        else
        {
            pathV2.GetLast().GetElement().transform.position = new Vector3(pathV2.SelectInd(i).GetElement().transform.position.x, pathV2.SelectInd(i).GetElement().transform.position.y, pathV2.SelectInd(i).GetElement().transform.position.z + distMove);
            //Setting initial movement direction
            if (i == 0)
            {
                sphereScript.l_or_r = false;
            }
        }
    }
    if (pathV2.SelectInd(randIndex).GetNext().GetElement().transform.position.x > 
        pathV2.SelectInd(randIndex).GetElement().transform.position.x)
    {
        if (pathV2.SelectInd(randIndex).GetNext(2).GetElement().transform.position.z > pathV2.SelectInd(randIndex).GetElement().transform.position.z){
            fakeCube.transform.position = new Vector3(pathV2.SelectInd(randIndex).GetNext().GetElement().transform.position.x + distMove, 
                                                      pathV2.GetSelect().GetElement().transform.position.y, 
                                                      pathV2.SelectInd(randIndex).GetElement().transform.position.z);
        }else{
            fakeCube.transform.position = new Vector3(pathV2.SelectInd(randIndex).GetElement().transform.position.x, 
                                                      pathV2.GetSelect().GetElement().transform.position.y, 
                                                      pathV2.SelectInd(randIndex).GetElement().transform.position.z + distMove);
        }
    }else{
        if (pathV2.SelectInd(randIndex).GetNext(2).GetElement().transform.position.x > pathV2.SelectInd(randIndex).GetNext().GetElement().transform.position.x){
            fakeCube.transform.position = new Vector3(pathV2.SelectInd(randIndex).GetNext().GetElement().transform.position.x, 
                                                      pathV2.GetSelect().GetElement().transform.position.y, 
                                                      pathV2.SelectInd(randIndex).GetElement().transform.position.z + distMove);
        }else{
            fakeCube.transform.position = new Vector3(pathV2.SelectInd(randIndex).GetElement().transform.position.x + distMove, 
                                                      pathV2.GetSelect().GetElement().transform.position.y, 
                                                      pathV2.SelectInd(randIndex).GetElement().transform.position.z);
        }
    }
    isGenerated = false;
    cubeX = pathV2.GetLast().GetElement().transform.position.x;
    cubeZ = pathV2.GetLast().GetElement().transform.position.z;

    updatedX = cubeX;
    updatedZ = cubeZ;
}

// Update is called once per frame
void Update()
{
    SpawnCube();
}

private void SpawnCube()
{
    float random = Random.value;
    //Updates path to move cubes by seeing if (position of the first cube) < ((position of the sphere) - however long the trail of cubes should be)
    if ((pathV2.GetSelect().GetElement().transform.position.x + pathV2.GetSelect().GetElement().transform.position.z) < ((sphere.transform.position.x + sphere.transform.position.z) - 5.0f))
    {
        if (random <= 0.5f)
        {
            updatedX = updatedX + distMove;
            pathV2.GetSelect().GetElement().transform.position = new Vector3(updatedX, pathV2.GetSelect().GetPrev().GetElement().transform.position.y, updatedZ);
        }
        else
        {
            updatedZ = updatedZ + distMove;
            pathV2.GetSelect().GetElement().transform.position = new Vector3(updatedX, pathV2.GetSelect().GetPrev().GetElement().transform.position.y, updatedZ);
        }
        pathV2.Rotate(1);
    }


    if(isGenerated == false){
        randIndex = pathV2.SelectInd(prevIndex).GetNext(10).GetIndex();
        prevIndex = randIndex;
        isGenerated = true;
        Debug.Log("Random Index: " + randIndex);
    }

    if ((fakeCube.transform.position.x + fakeCube.transform.position.z) + 2 < (sphere.transform.position.x + sphere.transform.position.z) && 
        ((pathV2.SelectInd(prevIndex).GetElement().transform.position.x + pathV2.SelectInd(prevIndex).GetElement().transform.position.z) > (sphere.transform.position.x + sphere.transform.position.z))
       ){
        if (pathV2.SelectInd(prevIndex).GetNext().GetElement().transform.position.x > pathV2.SelectInd(prevIndex).GetElement().transform.position.x){
            if(pathV2.SelectInd(prevIndex).GetNext(2).GetElement().transform.position.z > pathV2.SelectInd(prevIndex).GetElement().transform.position.z){
                fakeCube.transform.position = new Vector3(pathV2.SelectInd(prevIndex).GetNext().GetElement().transform.position.x+distMove, 
                                                          pathV2.GetSelect().GetElement().transform.position.y, 
                                                          pathV2.SelectInd(prevIndex).GetElement().transform.position.z);
            }
            else{
                fakeCube.transform.position = new Vector3(pathV2.SelectInd(prevIndex).GetElement().transform.position.x,
                                                          pathV2.GetSelect().GetElement().transform.position.y, 
                                                          pathV2.SelectInd(prevIndex).GetElement().transform.position.z + distMove);
            }
        }else{
            if (pathV2.SelectInd(randIndex).GetNext(2).GetElement().transform.position.x > pathV2.SelectInd(prevIndex).GetNext().GetElement().transform.position.x)
            {
                fakeCube.transform.position = new Vector3(pathV2.SelectInd(prevIndex).GetNext().GetElement().transform.position.x, 
                                                          pathV2.GetSelect().GetElement().transform.position.y, 
                                                          pathV2.SelectInd(prevIndex).GetElement().transform.position.z + distMove);
            }
            else
            {
                fakeCube.transform.position = new Vector3(pathV2.SelectInd(prevIndex).GetElement().transform.position.x + distMove, 
                                                          pathV2.GetSelect().GetElement().transform.position.y, 
                                                          pathV2.SelectInd(prevIndex).GetElement().transform.position.z);
            }
        }
        isGenerated = false;
    }
}

When this bit runs, it freezes:

if ((fakeCube.transform.position.x + fakeCube.transform.position.z) + 2 < (sphere.transform.position.x + sphere.transform.position.z) && 
        ((pathV2.SelectInd(prevIndex).GetElement().transform.position.x + pathV2.SelectInd(prevIndex).GetElement().transform.position.z) > (sphere.transform.position.x + sphere.transform.position.z))
       ){
        if (pathV2.SelectInd(prevIndex).GetNext().GetElement().transform.position.x > pathV2.SelectInd(prevIndex).GetElement().transform.position.x){
            if(pathV2.SelectInd(prevIndex).GetNext(2).GetElement().transform.position.z > pathV2.SelectInd(prevIndex).GetElement().transform.position.z){
                fakeCube.transform.position = new Vector3(pathV2.SelectInd(prevIndex).GetNext().GetElement().transform.position.x+distMove, 
                                                          pathV2.GetSelect().GetElement().transform.position.y, 
                                                          pathV2.SelectInd(prevIndex).GetElement().transform.position.z);
            }
            else{
                fakeCube.transform.position = new Vector3(pathV2.SelectInd(prevIndex).GetElement().transform.position.x,
                                                          pathV2.GetSelect().GetElement().transform.position.y, 
                                                          pathV2.SelectInd(prevIndex).GetElement().transform.position.z + distMove);
            }
        }else{
            if (pathV2.SelectInd(randIndex).GetNext(2).GetElement().transform.position.x > pathV2.SelectInd(prevIndex).GetNext().GetElement().transform.position.x)
            {
                fakeCube.transform.position = new Vector3(pathV2.SelectInd(prevIndex).GetNext().GetElement().transform.position.x, 
                                                          pathV2.GetSelect().GetElement().transform.position.y, 
                                                          pathV2.SelectInd(prevIndex).GetElement().transform.position.z + distMove);
            }
            else
            {
                fakeCube.transform.position = new Vector3(pathV2.SelectInd(prevIndex).GetElement().transform.position.x + distMove, 
                                                          pathV2.GetSelect().GetElement().transform.position.y, 
                                                          pathV2.SelectInd(prevIndex).GetElement().transform.position.z);
            }
        }
        isGenerated = false;
    }

Any help as to what to do, and maybe tips, that would be awesome.

\$\endgroup\$
  • 1
    \$\begingroup\$ Have you tried setting a breakpoint and stepping through the code to see what steps it's performing during the delay? \$\endgroup\$ – DMGregory Nov 19 '18 at 2:14
  • \$\begingroup\$ I have not, I'll give that a go. \$\endgroup\$ – Decapitated Rainbow Nov 19 '18 at 3:06
  • \$\begingroup\$ Unrelated tip, I strongly recommend not using GameObject.Find, if you rename your gameobject in the hierarchy your script will break. Use a public (or [SerializeField] private) field and assign the reference in the inspector. \$\endgroup\$ – mr-matt Nov 19 '18 at 4:22
1
\$\begingroup\$

I originally started with an array, everything worked fine. I changed to a Linked List to allow for easier implementation of the path. But I decided to switch back to the Array implementation. I was reading online that Linked List's are slower than an array, especially with MonoBehavior. And what I assume, is that it's so much slower, that when I would run that piece of code, it would take much longer than the Array. To the point where it would freeze until it finished. I took @DMGregory advice to use a breakpoint to see what my code was doing. And it worked normally, but with the extended time it took to finish.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.