Search Unity

How do i fix this infinite loop?

Discussion in 'Scripting' started by ApoletStudios, Mar 8, 2017.

  1. ApoletStudios

    ApoletStudios

    Joined:
    Mar 8, 2017
    Posts:
    2
    I am new in C# and Unity, I would love an explanation of the issue but simple fix would be just fine. Thank you.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class moveT : MonoBehaviour {
    6.  
    7.     public GameObject treeFab;                                 //The tree game object.
    8.     public int treePoolSize;                           //How many trees to keep on standby.
    9.                                                               //How quickly trees spawn.
    10.    
    11.     private GameObject[] trees;                                   //Collection of pooled trees.
    12.                                                                
    13.     private Vector3[] pos1rot1;                        //Collection of pooled Positions.
    14.  
    15.    
    16.  
    17.    
    18.     void Start ()
    19.     {
    20.        
    21.         trees = new GameObject[treePoolSize];
    22.         pos1rot1 = new Vector3[5] ;
    23.         int i;
    24.         for (i = 0; i < 5; i++)
    25.        
    26.             pos1rot1[i] = new Vector3 (-123+10*i, 27-i, 74+3*i);
    27.  
    28.        
    29.        
    30.         for ( i = 0; i < treePoolSize; i++)
    31.         {
    32.            
    33.             trees[i] = (GameObject)Instantiate(treeFab, pos1rot1[i % 5], Quaternion.identity);
    34.  
    35.         }
    36.            
    37.                      
    38.            
    39.        
    40.     }
    41.    
    42.     private int index = 0;
    43.    
    44.     void Update ()
    45.     {
    46.         if (gameControl.instance.gameState == 1 && (trees[index].transform.position.z < -13f))
    47.         {
    48.            
    49.             trees[index].transform.position = pos1rot1[Random.Range(0,5)];
    50.             index++;
    51.         }
    52.         if (index >= treePoolSize)
    53.             index = 0;
    54.  
    55.     }
    56. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    7,084
    I don't see any code there that could cause an infinite loop. I see some weird concepts going on (why are you setting the position in Update's, after they are spawned?), but no infinite loop potential. It's possible that Start() could take a long time if your treePoolSize is really big, but not to the point of freezing Unity.

    I can think of 2 possibilities:
    1) It's another script entirely that's causing the infinite loop.
    2) This script is on the prefab that's being instantiated. So this script generates a tree, which has this script, which generates a tree, which has this script, which.....
     
    ApoletStudios likes this.
  3. ApoletStudios

    ApoletStudios

    Joined:
    Mar 8, 2017
    Posts:
    2
    2) This script is on the prefab that's being instantiated. So this script generates a tree, which has this script, which generates a tree, which has this script, which.....

    That was the problem, thanks a lot man:)
     
  4. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    This will eat whole performance. Forget this way.
     
  5. ShokeR0

    ShokeR0

    Joined:
    Feb 24, 2016
    Posts:
    112
    The guy just trying to learn the very basics, he can worry about that much.. much later.
     
unityunity