Search Unity

Question Asigning a Prefab to a Script overloads Unitys Application run

Discussion in 'Prefabs' started by AndreasAna, Oct 11, 2022.

  1. AndreasAna

    AndreasAna

    Joined:
    Mar 21, 2022
    Posts:
    4
    Hello,

    Since im quite new in development and programming, i dont know much about how things are actualy called.

    I started a Project, made the Joystick and worked on the camera to fit my needs and it all worked out well. My next point on the list was to make a ground Instantiator that spawns the ground on game start depending on the used resolution on awake that works in a script from the camera. Writing the script was quite simple and runing tests on it worked only under one condition: to not have a prefab attached to the script... having a prefab attached on the public gameobject ground to instantiate it make unity unable to even run the game. Overloading the whole computer and filling the memory slowly but without even reaching a started playmode.
    This made me think that its more about unity itself then about the script that might make problems.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GroundInstanciator : MonoBehaviour
    6. {
    7.     public GameObject ground;
    8.     private Transform Ground;
    9.     private float groundSizeX;
    10.     private float groundSizeY;
    11.  
    12.     private float screenX;
    13.     private float screenY;
    14.    
    15.     private int groundAmountX;
    16.     private int groundAmountY;
    17.  
    18.     public float edgeX;
    19.     public float edgeY;
    20.  
    21.     void Start()
    22.     {
    23.         Ground = GameObject.Find("Ground").transform;
    24.         groundSizeX = ground.GetComponent<BoxCollider2D>().size.x;
    25.         groundSizeY = ground.GetComponent<BoxCollider2D>().size.y;
    26.         screenX = GameObject.Find("Main Camera").GetComponent<ScreenView>().viewX;
    27.         screenY = GameObject.Find("Main Camera").GetComponent<ScreenView>().viewY;
    28.         groundAmountX = (int)groundSizeX * (int)(screenX / groundSizeX + 2);
    29.         groundAmountY = (int)groundSizeY * (int)(screenY / groundSizeY + 2);
    30.         for(int i = -groundAmountX; i < groundAmountX; ++i)
    31.         {
    32.             for (int j = -groundAmountY; i < groundAmountY; ++j)
    33.             {
    34.                 Vector3 pos = new Vector3(i * groundSizeX, j * groundSizeY, 0);
    35.                 Instantiate(ground, pos, Quaternion.identity, Ground);
    36.             }
    37.         }
    38.         edgeX = screenX + groundSizeX;
    39.         edgeY = screenY + groundSizeY;
    40.     }
    41. }
    thats the whole script. Attaching a prefab on the public GameObject ground via the inspector made Unity unable to start a tryrun. (im talking not about building and running a game, im talking about pressing the playbutton in the middle top of unity editor.)

    please help since i cant see my problem.
     
  2. bloodthirst69

    bloodthirst69

    Joined:
    Oct 1, 2017
    Posts:
    28
    i think you have a typo in your for loop that has nothing to do with prefabs.
    The inner for loop that uses the "int j" as index is comparing "i" with "groundAmountY" instead of "j".
    This causes an infinite loop because in the inner loop "i" never changes its value , so the "i < groundAmountY" condition will always be true and the logic inside it wil run forever until a crash happens