First off hello everyone !
The question I have is connected to a small space strategy game I am developing similar to MOO series, Endless Space, Galactic Civ series etc… Of course with a much more minimalistic design than most of these.
Anyway’s the problem I am currently facing and have been banging my head against is saving the planets and suns to the correct star in the “Galaxy Screen”. Now that’s tricky since the planets and suns are on a separate scene “Solar System”. I am also trying to do it without having to save and recall data since I plan for this to be an online game.
So far I have gotten the stars in the “Galaxy screen” to randomly generate after which the script remembers which stars have been generated it also gives them a specific number for ID. From there you can go in to any star on the “Galaxy screen” and a solar system will randomly generate.
Now here is where things get buggy. If I go from the “Galaxy screen” to any star it will generate and load a solar system, if i go back to the “Galaxy Screen” at this point don’t go into any other stars(solar systems), but instead go back to the same star that we just exited the sun and planets will still be the same (the script remembers that we have created and attached that solar system to a specific “Galaxy screen” star). But if I go to a second star and try to come back the script “forgets” the assigned planets and suns leaves them inactive and creates new random ones. Here is the Random Planet Generator script (The one for the suns is pretty much the same.):
using UnityEngine;
using System.Collections;
public class RandomPlanetGenerate : MonoBehaviour {
public GameObject [] Planets;
public GameObject thisplanet;
public int randomplanet;
public int SystemID;
public bool PlanetCreation = false;
public static bool hasplanetbeencreated = false;
IEnumerator Timer()
{
yield return new WaitForSeconds (0.1f);
hasplanetbeencreated = true;
}
void Awake () {
if (hasplanetbeencreated == true) {
Destroy (gameObject);
}
if (SystemID != RedStar.MouseOverStarNumber ) {
randomplanet = Random.Range (0, 14);
thisplanet = (GameObject)Instantiate (Planets [randomplanet], transform.position, Quaternion.identity);
thisplanet.transform.parent = gameObject.transform;
PlanetCreation = true;
StartCoroutine (Timer ());
}
}
void Start () {
SystemID = RedStar.MouseOverStarNumber;
if (SystemID != RedStar.MouseOverStarNumber) {
PlanetCreation = false;
}
}
// Update is called once per frame
void Update () {
DontDestroyOnLoad (gameObject);
if (SystemID == RedStar.MouseOverStarNumber && Application.loadedLevelName == "StarSystemx5") {
thisplanet.SetActive (true);
}
if (SystemID != RedStar.MouseOverStarNumber) {
thisplanet.SetActive (false);
}
if (Application.loadedLevelName == "StarSystemx5" && SystemID != RedStar.MouseOverStarNumber) {
PlanetCreation = false;
SystemID = RedStar.MouseOverStarNumber;
}
if (PlanetCreation == false && Application.loadedLevelName == "StarSystemx5") {
randomplanet = Random.Range (0, 14);
thisplanet = (GameObject)Instantiate (Planets [randomplanet], transform.position, Quaternion.identity);
thisplanet.transform.parent = gameObject.transform;
PlanetCreation = true;
}
}
}