(TL:DR "My prefab is supposed to create another one of itself if you keep going in a direction, but it only does this in one direction(the one you go to first), the other direction doesn’t work)
So, I have my first game where a plane flies around
I want some clouds to appear behind the plane but they only generate in front of the plane and get deleted behind it.
I have done this by having a background prefab, a left border, and an instantiate position. The prefab contains the background, the left border, and the instantiate position.
Once the player reaches the left border’s x position, the prefab instantiates at the instantiate position. This works pretty well, however, when I tried to copy and paste the same script (with some changes for the right side), and another border and instantiate position for the right side, it doesn’t work.
It lets me go one direction and it looks like it’s supposed to, but when I go the opposite direction, it doesn’t, it’s weird.
Here is the code for the left side:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoopLeftTest : MonoBehaviour
{
private GameObject empty;
private GameObject Player;
private GameObject right;
private bool dio = true;
private GameObject bor;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Player = GameObject.Find("Player");
empty = GameObject.Find("Empty");
right = GameObject.Find("Lefto");
bor = gameObject;
if (Player.transform.position.x < bor.transform.position.x + 2)
{
if (Player.transform.position.x > bor.transform.position.x - 2)
{
if (dio == true)
{
Instantiate(Resources.Load("SkyPreFab"), right.transform.position, empty.transform.rotation);
dio = false;
}
}
}
}
}
and here’s the code for the right side:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoopRightTest : MonoBehaviour
{
private GameObject empty;
private GameObject Player;
private GameObject right;
private bool dio = true;
private GameObject bor;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Player = GameObject.Find("Player");
empty = GameObject.Find("Empty");
right = GameObject.Find("Righto");
bor = gameObject;
if (Player.transform.position.x < bor.transform.position.x + 2)
{
if (Player.transform.position.x > bor.transform.position.x - 2)
{
if (dio == true)
{
Instantiate(Resources.Load("SkyPreFab"), right.transform.position, empty.transform.rotation);
dio = false;
}
}
}
}
}
When I go one direction, it’s supposed to loop, and it does, (one of these scripts keeps activating), but when I go the opposite direction, it doesn’t loop!
Thank you very much
-Spaceface