Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Tiling script spawning infinite clones

Discussion in '2D' started by Kiri Kaneko, Jul 31, 2015.

  1. Kiri Kaneko

    Kiri Kaneko

    Joined:
    Jul 18, 2015
    Posts:
    4
    I've been using a video series to learn the basics of making a 2D platformer:



    I followed his work and there were some errors, so I checked it and fixed a bunch of stuff, then I checked it again and again and it's definitely exactly what he wrote.

    The problem is that, as you can see from the attached picture, when I press play it starts spawning infinite clone foregrounds

    The script is supposed to check if the foreground needs to be extended, and then extend it if it does. Then it checks to see if it has been extended, which it has, and so it ends the script. However, it seems the check is somehow failing so the script repeats itself, spawning 2 more dirt foregrounds once per frame and I end up with infinite foregrounds to the left and right of the screen spawning on top of each other.

    This is the only discrepancy between my work and his though. In the first video he demonstrated that pressing jump repeatedly allowed for infinite jumping because the character was colliding with itself and showed how to fix this, but for me I couldn't infinite jump in the first place and didn't need to change anything. His character also has a couple of orange boxes on the top and bottom saying where it's ground and ceiling check is but mine doesn't have those boxes.

    Since it was working anyway I didn't dwell on it, and the script IS spawning new foregrounds, but I'm concerned that it might end up crashing itself, or at least be poorly optimized as the longer the game goes the more of these things are going to be flooding the heirarchy. How do I fix this?

    Here's my script by the way:

    using UnityEngine;
    using System.Collections;

    [RequireComponent (typeof(SpriteRenderer))]

    public class Tiling : MonoBehaviour {

    public int offsetX = 2; // the offset so that we don't get any weird errors

    // these are used for checking if I need to instantiate stuff
    public bool hasARightBuddy = false;
    public bool hasALeftBuddy = false;

    public bool reverseScale = false; // used if the object is not tileable

    private float spriteWidth = 0f; // the width of our element
    private Camera cam;
    private Transform myTransform;

    void Awake () {
    cam = Camera.main;
    myTransform = transform;
    }

    // Use this for initialization
    void Start () {
    SpriteRenderer sRenderer = GetComponent<SpriteRenderer>();
    spriteWidth = sRenderer.sprite.bounds.size.x;
    }

    // Update is called once per frame
    void Update () {
    // does it still need buddies? If not do nothing
    if (hasALeftBuddy == false || hasARightBuddy == false) {
    // calculate the cameras extend (half the width) of what the camera can see in world coordinates
    float camHorizontalExtend = cam.orthographicSize * Screen.width/Screen.height;

    // calculate the x position where the camera can see the edge of the sprite (element)
    float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth/2) - camHorizontalExtend;
    float edgeVisiblePositionLeft = (myTransform.position.x - spriteWidth/2) + camHorizontalExtend;

    //checking if we can see the edge of the element and then calling MakeNewBuddy if we can
    if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)
    {
    MakeNewBuddy (1);
    hasARightBuddy = true;
    }
    else if (cam.transform.position.x >= edgeVisiblePositionLeft + offsetX && hasALeftBuddy == false)
    {
    MakeNewBuddy (-1);
    hasALeftBuddy = true;
    }
    }
    }

    // a function that creates a buddy on the side required
    void MakeNewBuddy (int rightOrLeft) {
    // calculating the new position for our new buddy
    Vector3 newPosition = new Vector3 (myTransform.position.x + spriteWidth * rightOrLeft, myTransform.position.y, myTransform.position.z);
    // instantiating our new buddy and storing him in a variable
    Transform newBuddy = Instantiate (myTransform, newPosition, myTransform.rotation) as Transform;

    // if not tileable lets reverse the x size of our object to get rid of ugly seams
    if (reverseScale == true) {
    newBuddy.localScale = new Vector3 (newBuddy.localScale.x*-1, newBuddy.localScale.y, newBuddy.localScale.z);
    }

    newBuddy.parent = myTransform.parent;
    if (rightOrLeft > 0) {
    newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
    }
    else {
    newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
    }
    }
    }
     

    Attached Files:

    LaconicGames likes this.
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Please use CODE tags to make your code more readable here on the forums.
     
    LaconicGames likes this.
  3. Kiri Kaneko

    Kiri Kaneko

    Joined:
    Jul 18, 2015
    Posts:
    4
    What do you mean CODE tags? Sorry I'm new to this and these things don't come easily to me which is why i am using tutorials
     
    LaconicGames likes this.
  4. LaconicGames

    LaconicGames

    Joined:
    Aug 12, 2017
    Posts:
    4
    Blizzy means to paste the code part of the comment section that says 'Code: ' with a little document next to it. Bit of a late comment hahahah. And there was also a small fixable error in your code; 'if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX'

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent (typeof(SpriteRenderer))]
    5.  
    6. public class Tiling : MonoBehaviour {
    7.  
    8. public int offsetX = 2; // the offset so that we don't get any weird errors
    9.  
    10. // these are used for checking if I need to instantiate stuff
    11. public bool hasARightBuddy = false;
    12. public bool hasALeftBuddy = false;
    13.  
    14. public bool reverseScale = false; // used if the object is not tileable
    15.  
    16. private float spriteWidth = 0f; // the width of our element
    17. private Camera cam;
    18. private Transform myTransform;
    19.  
    20. void Awake () {
    21. cam = Camera.main;
    22. myTransform = transform;
    23. }
    24.  
    25. // Use this for initialization
    26. void Start () {
    27. SpriteRenderer sRenderer = GetComponent<SpriteRenderer>();
    28. spriteWidth = sRenderer.sprite.bounds.size.x;
    29. }
    30.  
    31. // Update is called once per frame
    32. void Update () {
    33. // does it still need buddies? If not do nothing
    34. if (hasALeftBuddy == false || hasARightBuddy == false) {
    35. // calculate the cameras extend (half the width) of what the camera can see in world coordinates
    36. float camHorizontalExtend = cam.orthographicSize * Screen.width/Screen.height;
    37.  
    38. // calculate the x position where the camera can see the edge of the sprite (element)
    39. float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth/2) - camHorizontalExtend;
    40. float edgeVisiblePositionLeft = (myTransform.position.x - spriteWidth/2) + camHorizontalExtend;
    41.  
    42. //checking if we can see the edge of the element and then calling MakeNewBuddy if we can
    43. if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)
    44. {
    45. MakeNewBuddy (1);
    46. hasARightBuddy = true;
    47. }
    48. else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && hasALeftBuddy == false)
    49. {
    50. MakeNewBuddy (-1);
    51. hasALeftBuddy = true;
    52. }
    53. }
    54. }
    55.  
    56. // a function that creates a buddy on the side required
    57. void MakeNewBuddy (int rightOrLeft) {
    58. // calculating the new position for our new buddy
    59. Vector3 newPosition = new Vector3 (myTransform.position.x + spriteWidth * rightOrLeft, myTransform.position.y, myTransform.position.z);
    60. // instantiating our new buddy and storing him in a variable
    61. Transform newBuddy = Instantiate (myTransform, newPosition, myTransform.rotation) as Transform;
    62.  
    63. // if not tileable lets reverse the x size of our object to get rid of ugly seams
    64. if (reverseScale == true) {
    65. newBuddy.localScale = new Vector3 (newBuddy.localScale.x*-1, newBuddy.localScale.y, newBuddy.localScale.z);
    66. }
    67.  
    68. newBuddy.parent = myTransform.parent;
    69. if (rightOrLeft > 0) {
    70. newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
    71. }
    72. else {
    73. newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
    74. }
    75. }
    76. }