So I’m trying to set up a mechanic where enemies spawn in a certain range based on how many keys you have. However, I had put the keys obtained counter in the PlayerController script that’s attached to the Player object because of the OnTriggerEnter code. In the SpawnManager, I want to reference the number of keysObtained, however I’m not sure what to do. I read that static makes the variable the same no matter the class, but it made the keysObtained counter disappear in the Unity UI and no enemies spawn (one would spawn before since I have a crappy work around I gave up on to look into the static route). Here’s my SpawnManager script (I know its a mess, I’m new to game development, just please be gentle)
EDIT: idk why the code looks like this. The code looks properly formatted for me in the edit question screen so I’m not sure what’s happening to it
public GameObject keyPrefab;
public GameObject enemyPrefab;
public int keyCount;
public int enemyCount;
float keysObtained = PlayerController.keysObtained;
private float leftSpawnRangeX = 22.0f;
private float rightSpawnRangeX = -19.0f;
private float lowerSpawnRangeY = 10.5f;
private float upperSpawnRangeY = 29.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
keyCount = FindObjectsOfType<Key>().Length;
enemyCount = FindObjectsOfType<EnemyMovement>().Length;
if (keyCount == 0)
{
SpawnKey();
}
if (enemyCount == 0 && keysObtained == 1)
{
SpawnEnemy();
} else if (enemyCount == 1 && keysObtained == 3)
{
SpawnEnemy();
} else if (enemyCount == 2 && keysObtained == 5)
{
SpawnEnemy();
} else if (enemyCount == 3 && keysObtained == 8)
{
SpawnEnemy();
} else if (enemyCount == 4 && keysObtained == 10)
{
SpawnEnemy();
}
}
private void SpawnKey()
{
Instantiate(keyPrefab, GenerateRandomSpawn(), keyPrefab.transform.rotation);
}
private void SpawnEnemy()
{
Instantiate(enemyPrefab, GenerateRandomSpawn(), enemyPrefab.transform.rotation);
}
private Vector3 GenerateRandomSpawn()
{
float SpawnPosX = Random.Range(leftSpawnRangeX, rightSpawnRangeX);
float SpawnPosY = Random.Range(lowerSpawnRangeY, upperSpawnRangeY);
Vector3 randomPos = new Vector3(SpawnPosX, SpawnPosY, 0);
return randomPos;
}
}