Script that manages all the variables depending on the difficulty doesnt update on all the scripts using those variables.
Hello.
So I have a "GameManager" game object that has a script which has all the variables used throughout all the components and game objects of the game that change depending on the difficulty you select with a switch case in its Start() function (I haven't balanced the game yet as it doesn't work):
public void Start()
{
difficulty = (int)GameDifficulty.Normal;
switch (difficulty)
{
case 0: //Easy
health = 100f;
enemyHealth = 20f;
fireRate = 0.2f;
reloadRate = 1f;
enemyMovementSpeed = 5f;
bulletSpeed = 2f;
invincibilityTimer = 1f;
eSpawnTime = 5f;
oSpawnTime = 10f;
orbSpeed = 1f;
LerpSpeed = 0.4f;
break;
case 1: //Medium
health = 100f;
enemyHealth = 20f;
fireRate = 0.2f;
reloadRate = 1f;
enemyMovementSpeed = 5f;
bulletSpeed = 2f;
invincibilityTimer = 1f;
eSpawnTime = 5f;
oSpawnTime = 10f;
orbSpeed = 1f;
LerpSpeed = 0.4f;
break;
case 2: //Hard
health = 100f;
enemyHealth = 20f;
fireRate = 0.2f;
reloadRate = 1f;
enemyMovementSpeed = 5f;
bulletSpeed = 2f;
invincibilityTimer = 1f;
eSpawnTime = 5f;
oSpawnTime = 10f;
orbSpeed = 1f;
LerpSpeed = 0.4f;
break;
case 3: //Pro
health = 100f;
enemyHealth = 20f;
fireRate = 0.2f;
reloadRate = 1f;
enemyMovementSpeed = 5f;
bulletSpeed = 2f;
invincibilityTimer = 1f;
eSpawnTime = 5f;
oSpawnTime = 10f;
orbSpeed = 1f;
LerpSpeed = 0.4f;
break;
}
I cant seem to use any of the variables throughout the game even though I create instances of of this script.
For another example, I want to lerp my instantiated player object into the scene and once it reaches where I want it to reach I have a bool that tells another script to start spawning enemies and power ups:
public void Update() //Player movement Update function
{
if (hasReached == false)
{
LerpValue += Time.deltaTime * 0.4f;
transform.position = Vector3.Lerp(spawnPos, center, LerpValue);
if (LerpValue > 1)
{
hasReached = true;
}
}
movement.x = Input.GetAxis("Horizontal");
movement.y = Input.GetAxis("Vertical");
}
and:
public void Update() //GameObjectSpawner Update function
{
if (pm.hasReached)
{
eSpawnTimer += Time.deltaTime;
oSpawnTimer += Time.deltaTime;
if (eSpawnTimer > GS.eSpawnTime)
{
EnemySpawner();
eSpawnTimer = 0;
}
if (oSpawnTimer > GS.oSpawnTime)
{
OrbSpawner();
oSpawnTimer = 0;
}
}
}
I have made every gameobject a prefab to be able to reference them all in the inspector. I'm very new to coding and unity so I know a lot of what I'm doing is wrong. I'm just trying to figure out how to use these variables across each component. Here are some of the scripts to make more sense if I haven't already:
public class GameObjectSpawner : MonoBehaviour //on the gamemanager prefab
{
public bool startGame, hasReached;
private float eSpawnTimer, oSpawnTimer; //Enemy and Orb
public Movement pm;
public GameSettings GS;
public GameObject playerPrefab, enemyPrefab, healthOrbPrefab, powerOrbPrefab, scoreOrbPrefab, deathOrbPrefab, invincOrbPrefab;
public Vector3 spawnPos, enemySpawnPos, enemySpawnPoint1, enemySpawnPoint2, enemySpawnPoint3, enemySpawnPoint4, orbSpawnPos, orbEndPos, orbSpawnPoint1, orbSpawnPoint2, orbSpawnPoint3, orbEndPoint1, orbEndPoint2, orbEndPoint3;
public void Start()
{
enemySpawnPoint1 = new Vector3(-2, 4, 0);
enemySpawnPoint2 = new Vector3(2, 4, 0);
enemySpawnPoint3 = new Vector3(-3, -4, 0);
enemySpawnPoint4 = new Vector3(3, -4, 0);
orbSpawnPoint1 = enemySpawnPoint1;
orbSpawnPoint2 = new Vector3(0, 4, 0);
orbSpawnPoint3 = enemySpawnPoint2;
orbEndPoint1 = enemySpawnPoint3;
orbEndPoint2 = new Vector3(0, -4, 0);
orbEndPoint3 = enemySpawnPoint4;
startGame = true;
if (startGame)
{
spawnPos = orbEndPoint2;
GameObject player = Instantiate(playerPrefab, spawnPos, Quaternion.identity);
}
}
public void Update()
{
if (pm.hasReached)
{
eSpawnTimer += Time.deltaTime;
oSpawnTimer += Time.deltaTime;
if (eSpawnTimer > GS.eSpawnTime)
{
EnemySpawner();
eSpawnTimer = 0;
}
if (oSpawnTimer > GS.oSpawnTime)
{
OrbSpawner();
oSpawnTimer = 0;
}
}
}
void EnemySpawner()
{
int spawnPosition = Random.Range(0, 3);
switch (spawnPosition)
{
case 0:
enemySpawnPos = enemySpawnPoint1;
break;
case 1:
enemySpawnPos = enemySpawnPoint2;
break;
case 2:
enemySpawnPos = enemySpawnPoint3;
break;
case 3:
enemySpawnPos = enemySpawnPoint4;
break;
}
GameObject enemy = Instantiate(enemyPrefab, enemySpawnPos, Quaternion.identity);
}
void OrbSpawner()
{
int spawnPosition = Random.Range(0, 2);
int orbSelector = Random.Range(0, 99);
switch (spawnPosition)
{
case 0:
orbSpawnPos = orbSpawnPoint1;
orbEndPos = orbEndPoint1;
break;
case 1:
orbSpawnPos = orbSpawnPoint2;
orbEndPos = orbEndPoint2;
break;
case 2:
orbSpawnPos = orbSpawnPoint3;
orbEndPos = orbEndPoint3;
break;
}
if (orbSelector < 20) //all 20% chance for now
{
GameObject orb = Instantiate(powerOrbPrefab, orbSpawnPos, Quaternion.identity); //red
}
else if (orbSelector > 19 && orbSelector < 40)
{
GameObject orb = Instantiate(healthOrbPrefab, orbSpawnPos, Quaternion.identity); //green
}
else if (orbSelector > 39 && orbSelector < 60)
{
GameObject orb = Instantiate(deathOrbPrefab, orbSpawnPos, Quaternion.identity); //purple
}
else if (orbSelector > 59 && orbSelector < 80)
{
GameObject orb = Instantiate(invincOrbPrefab, orbSpawnPos, Quaternion.identity); //white
}
else
{
GameObject orb = Instantiate(scoreOrbPrefab, orbSpawnPos, Quaternion.identity); //gold
}
}
}
public class Movement : MonoBehaviour //on the player prefab
{
public float movementSpeed;
private float LerpValue;
public bool hasReached;
private Vector3 spawnPos;
public Vector3 center;
public Rigidbody2D rb;
private Vector2 movement;
public AiHealth enemy;
public OrbLogic orb;
public void Start()
{
spawnPos = transform.position;
center = new Vector3(0, -0.5f, 0);
}
public void Update()
{
if (hasReached == false)
{
LerpValue += Time.deltaTime * 0.4f;
transform.position = Vector3.Lerp(spawnPos, center, LerpValue);
if (LerpValue > 1)
{
hasReached = true;
}
}
movement.x = Input.GetAxis("Horizontal");
movement.y = Input.GetAxis("Vertical");
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + movement * movementSpeed * Time.fixedDeltaTime);
}
private void OnCollisionEnter2D(Collision2D collision)
{
AiHealth enemyHit = collision.gameObject.GetComponent<AiHealth>();
if (enemyHit != null)
{
enemyHit.ReceiveDamage();
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
OrbLogic orbHit = collision.gameObject.GetComponent<OrbLogic>();
if (orbHit != null)
{
orbHit.OrbActivation();
}
}
}
I apologise for the wall of text and I appreciate anyone that read this far. Thanks for any help.
Answer by streeetwalker · Mar 29, 2020 at 05:14 AM
@ga3486u, I think I understand what you want to do, but I don't see where in your scripts you are doing it. Let me explain one way of doing it that is the easiest:
A) make sure you only have one instance of GameManager
B) create a static class, for example, GameVariables, with public static variables for each of your variables. Do not put this class as a script component on any game object - it just sits in your assets folder!
// for example:
public static GameVariables {
public static float health;
public static float enemyHealth;
public static float fireRate;
public static float eSpawnTime;
// and so on
}
C) Where ever you want to reference any of these shared variables, preface it with the class name. For example:
case 0: //Easy
GameVariables.health = 100f;
GameVariables.enemyHealth = 20f;
// and so on ...
break;
// and
GameVariables.eSpawnTime += Time.DeltaTime;
Remember that these are shared, so setting a GameVariables.variable value will make the change available everywhere you access the same GameVariables.variable