@bgulanowski TY for the response. I still couldn’t solve problem;
Firstly, I used explicit private field you suggested; also deleted the null check from PlatformSpawner. I didn’t get a message from Debug.Log that I wrote in BackgroundSpawner 's SelectedBackground properties setter, also Debugger was showing that SelectedBackground property is either bg_0 or bg_1. When it comes to PlatformSpawner, it is null. It is still spawning just ONE platform at the start of the game then stops.
- Also there is no second BackgroundSpawner, never gets destroyed/recreated.
- And, no, SelectedBackground isn’t being set from any other script than BackgroundSpawner, also not being get from any other script than PlatformSpawner.
Secondly, I used two bool fields, here is updated “BackgroundSpawer” and “PlatformSpawner”. (I’m posting full scripts for both of them, there will be something that I missed.)
PlatformSpawner:
using System.Collections;
using UnityEngine;
public class PlatformSpawner : MonoBehaviour
{
[SerializeField] GameObject[] platform = new GameObject[2];
[SerializeField] EndlessPitBackgroundSpawner backgroundSpawner = null;
[SerializeField] Transform platformListRootObject = null;
[SerializeField] Transform spawnAreaL = null, spawnAreaR = null;
[SerializeField] float spawnInterval = 1f, spawnDeadZone = 0.1f;
[SerializeField] int spawnedPlatformLimit = 10;
int spawnedPlatformCount = 0;
void Start()
{
StartCoroutine(SpawnPlatform());
}
IEnumerator SpawnPlatform()
{
GameObject newPlatform = null;
Vector3 lastPos = Vector3.zero,
newPos = Vector3.zero;
bool isThisFirstLoop = true;
while(true)
{
yield return null;
Debug.Log("> " + this.gameObject.name + " () Platform Count: " + spawnedPlatformCount);
if (CheckIfPlatformLimitReached() == true)
continue;
newPos = CalculateNewPosition();
if (!isThisFirstLoop && CheckIfNewPositionInDeadZone(lastPos, newPos) == true)
continue;
newPlatform = InstantiateNewPlatform(newPos);
if (newPlatform == null)
continue;
lastPos = newPos;
if(isThisFirstLoop)
isThisFirstLoop = false;
yield return new WaitForSecondsRealtime(spawnInterval);
}
}
bool CheckIfPlatformLimitReached()
{
spawnedPlatformCount = platformListRootObject.childCount;
return (spawnedPlatformCount >= spawnedPlatformLimit) ? true : false;
}
Vector3 CalculateNewPosition()
{
return new Vector3(Random.Range(spawnAreaL.position.x, spawnAreaR.position.x),
spawnAreaL.position.y);
}
bool CheckIfNewPositionInDeadZone(Vector3 lastPos, Vector3 newPos)
{
Vector3 deltaVector = newPos - lastPos;
bool isNewXInDeadZone = (Mathf.Abs(deltaVector.x) <= spawnDeadZone) ? true : false;
bool isNewYInDeadZone = (Mathf.Abs(deltaVector.y) <= spawnDeadZone) ? true : false;
return (!isNewXInDeadZone && !isNewYInDeadZone) ? false : true;
}
GameObject InstantiateNewPlatform(Vector3 newPos)
{
GameObject selectedPlatform = null;
if (backgroundSpawner.BackgroundZeroSelected)
selectedPlatform = platform[1];
else if (backgroundSpawner.BackgroundOneSelected)
selectedPlatform = platform[0];
return Instantiate(selectedPlatform, newPos, Quaternion.identity, platformListRootObject.transform);
}
}
BackgroundSpawner:
using UnityEngine;
public class EndlessPitBackgroundSpawner : MonoBehaviour
{
[SerializeField] Transform bgList = null;
[SerializeField] GameObject[] bg = new GameObject[2];
private GameObject selectedBackground = null;
public GameObject SelectedBackground
{
get => selectedBackground;
private set
{
selectedBackground = value;
if (selectedBackground == null)
Debug.Log("> selectedBackground is NULL.");
}
}
public bool BackgroundZeroSelected { get; private set; }
public bool BackgroundOneSelected { get; private set; }
public GameObject InstantiateNewBackground(Transform bgSpawn)
{
int index = Random.Range(0, bg.Length);
BackgroundZeroSelected = (index == 0) ? true : false;
BackgroundOneSelected = !BackgroundZeroSelected;
SelectedBackground = bg[index];
return Instantiate(SelectedBackground, bgSpawn.position, Quaternion.identity, bgList);
}
}
This method doesn’t directly using SelectedBackground property, but result is same. Also I don’t know if it is important but, InstantiateNewBackground(…) method is being called by current background. When it reaches a point while moving down, it calls this method by giving bgSpawn argument that is child of background that calls this method. This way Backgrounds keep spawning one after one.
While waiting for a response I’m going to reverse the script to change referencing mechanism. Maybe adjusting which platform is going to be next from BackgroundSpawner script will make a difference.