Search Unity

Trouble with scenes

Discussion in 'Scripting' started by chemimartinez77, Feb 17, 2019.

  1. chemimartinez77

    chemimartinez77

    Joined:
    Apr 8, 2016
    Posts:
    25
    Hi all,

    I'm trying to develop a Risk-like game just for fun and learning. The thing is that it's getting too complicated for me right now. I didn't prepare the project and started coding things from scratch.

    Well, now I'm trying to separate the "attack phase" of the game from the rest just by making it into a new scene but I don't know how.
    If I use the
    Code (CSharp):
    1. void Awake () { DontDestroyOnLoad (transform.gameObject); }
    I don't know how to pass more objects to the scene.

    Watch video for further info.

    Tanks in advance!

     
  2. chrisrcisme

    chrisrcisme

    Joined:
    Jul 7, 2014
    Posts:
    16
    Looking at video I would create an empty game object called GameManager then create a new script on the GameManager object called gameManager or whatever you want to call them is fine.

    Then in the new script you can use it to reference the current Attacker and Defender and make the GameManager object to not destroy on load so it doesn't delete itself.

    At the same time as the scene is changed when selecting the two countries, you can get the reference from the gameManager script which holds latest references on the current Attacker and Defender (also assuming they are prefabs) you can then instantiate them this way into the new scene.

    Also instead of creating a new scene you could just change the area of the camera to a new position in the world for example MainGame starting at 0 , 0 ,0 and when two countries have been prepped and selected for war you can change the global position to be something like 100 , 0 , 100 depending what angle your game is being played at.

    I hope this helps. Let me know how it goes :)
     
  3. chemimartinez77

    chemimartinez77

    Joined:
    Apr 8, 2016
    Posts:
    25
    Hey, Chris. Thanks for your reply. I'm glad you did because I see it was your fifth post in 5 years lol :D

    I like both approaches. Maybe I like more the global reposition one, so I won't need to change the scenes. Anyway, I'm just learning and I don't care too much about clean code or doing things completely "correct", you know.

    Well, my first mistake is that "Country" (attacker and defender in this case) is not a prefab. But here comes another question. Can I make a prefab that dynamically changes the sprite on its image component? Because as you can guess, each country has its "shape".

    Thanks again for your reply.

    P.S.: I'll upload a video in this thread when I have finished the combat phase. Cheers!
     
    chrisrcisme likes this.
  4. chrisrcisme

    chrisrcisme

    Joined:
    Jul 7, 2014
    Posts:
    16
    That's a great way to learn fast by not doing things completely correct because there is always more than one way to do things while creating a game. I've only been learning myself too ( without giving up ) in the past 7-8 months. The more you practice using Unity and especially writing code the better you will get and I can see you are doing very well.

    "Can I make a prefab that dynamically changes the sprite on its image component?"
    Yes, you can do this using code by referencing the sprites from the project menu anywhere in the assets folder or subfolders.
    Also, I thought id explain that a prefab is basically like a template or ready-made object ready to be put into the game scene.

    One more thing I forgot to mention in my last post is that when you instantiate objects such as Attacker / Defender into the new scene this has to be referenced from the assets "prefab" folder or where ever it is from the project menu, not clicked and dragged from the game hierarchy onto your script reference.

    Good luck with finishing the combat phase :D

    If you need any more help with more scripting just post here ill be happy to take a look :)
     
    chemimartinez77 likes this.
  5. chemimartinez77

    chemimartinez77

    Joined:
    Apr 8, 2016
    Posts:
    25
    Thanks again. I'm reconsidering the project and now I'm almost at the combat phase again.

    See you in a few days!
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Breaking things apart by scenes is really awesome in Unity... you can also overlay multiple scenes (that is called 'additive loading,' or 'additively loaded') and control which ones get unloaded when.

    Keep in mind that if you are coding up a blob that loads a new scene, you CANNOT immediately use anything in that scene after loading it. You have to wait until the next frame. Scene loads issued in a given frame are not available until the NEXT frame.

    Obviously this does not matter to scripts actually contained in the new scene because obviously they will only exist once the scene exists, which is always on the next frame.