Tiling script spawning infinite clones

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();
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().hasALeftBuddy = true;
}
else {
newBuddy.GetComponent().hasARightBuddy = true;
}
}
}

1 Like

Please use CODE tags to make your code more readable here on the forums.

1 Like

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

1 Like

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’

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