Search Unity

Saving objects to a scene and reactivating them.

Discussion in 'Scripting' started by Bolchev, Feb 24, 2016.

  1. Bolchev

    Bolchev

    Joined:
    Aug 3, 2015
    Posts:
    17
    First off hello everyone ! :D
    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.):
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class RandomPlanetGenerate : MonoBehaviour {
    6.     public GameObject [] Planets;
    7.     public GameObject thisplanet;
    8.  
    9.     public int randomplanet;
    10.     public int SystemID;
    11.  
    12.     public bool PlanetCreation = false;
    13.     public static bool hasplanetbeencreated = false;
    14.     IEnumerator Timer()
    15.     {
    16.         yield return new WaitForSeconds (0.1f);
    17.         hasplanetbeencreated = true;
    18.     }
    19.     void Awake () {
    20.         if (hasplanetbeencreated == true) {
    21.             Destroy (gameObject);
    22.         }
    23.         if (SystemID != RedStar.MouseOverStarNumber ) {
    24.             randomplanet = Random.Range (0, 14);
    25.             thisplanet = (GameObject)Instantiate (Planets [randomplanet], transform.position, Quaternion.identity);
    26.             thisplanet.transform.parent = gameObject.transform;
    27.             PlanetCreation = true;
    28.             StartCoroutine (Timer ());
    29.         }
    30.  
    31.    
    32.     }
    33.     void Start () {
    34.         SystemID = RedStar.MouseOverStarNumber;
    35.         if (SystemID != RedStar.MouseOverStarNumber) {
    36.             PlanetCreation = false;
    37.         }
    38.         }
    39.    
    40.     // Update is called once per frame
    41.     void Update () {
    42.         DontDestroyOnLoad (gameObject);
    43.         if (SystemID == RedStar.MouseOverStarNumber && Application.loadedLevelName == "StarSystemx5") {
    44.             thisplanet.SetActive (true);
    45.         }
    46.         if (SystemID != RedStar.MouseOverStarNumber) {
    47.             thisplanet.SetActive (false);
    48.  
    49.        
    50.         }
    51.         if (Application.loadedLevelName == "StarSystemx5" && SystemID != RedStar.MouseOverStarNumber) {
    52.             PlanetCreation = false;
    53.             SystemID = RedStar.MouseOverStarNumber;
    54.         }
    55.        if (PlanetCreation == false && Application.loadedLevelName == "StarSystemx5") {
    56.             randomplanet = Random.Range (0, 14);
    57.             thisplanet = (GameObject)Instantiate (Planets [randomplanet], transform.position, Quaternion.identity);
    58.             thisplanet.transform.parent = gameObject.transform;
    59.             PlanetCreation = true;
    60.         }
    61.     }
    62. }
    63.  
    64.  
     
  2. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Hi,

    You better split your game into a client and a server, and morever that you are making and online game. It will simplified your problems a lot and you will avoid a lot of headache. The server will be the part that store all the data of your game and it will run online somewhere on the Internet and the client will be running on your players machines and will communicate with the server to get/update data.
     
    Last edited: Feb 24, 2016
  3. Bolchev

    Bolchev

    Joined:
    Aug 3, 2015
    Posts:
    17
    Hello thank you for the quick response! I have developed a few games so far with open graphics just to learn the basics Unity but I have never developed an online game, before in fact I started this one to learn about creating such games. Would you recommend something like Photon Unity Networking for a beginner or is there a better way I also read that Unity itself has some kind of feature to make a server (if I haven't misunderstood) or is there a better way than both of those ?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  5. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    I have experience with low level networking and web technologies. I don't really know about the integrated Unity networking system. I heard that Photon is nice, but I can't tell more than the reviews on the Asset Store. But I would advice to learn about general networking concepts like: client/server, protocols, transactions, synchronization, ... and that before diving into any code.