Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How does it work?

Discussion in '2D' started by YoYoYoYo_111_PiPi, Oct 9, 2016.

  1. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    Hey, so I'm interested in creating a 2D Point and Click adventure game. I'm new to Unity and I am still checking out the basics... how the scenes work, GUI etc.

    So my question is how to update scenes, as in... If we have an object that is a part of a certain scene, and while we are in that scene, we can pick it up, so we can carry it on to another scene and use it there... I don't know, there's a character that needs it or something.

    But how do we make the game memorize the change, because from what I understand, scenes always restart themselves, and what's in them in the beginning will always be there, whenever we leave and reenter the same scene. How to make a change, so that, once we picked that certain object... once we leave that scene and return back to it, it's no longer there, cause it's in our inventory... How do we change scene content via gameplay?

    Now, this all might be a simple matter, but from what I saw with how audio files, buttons, animations work, it makes no sense as to how to carry around an object from scene to scene... And how the change in one scene can affect all the other scenes that are in the game.

    If someone could advise the right direction on this matter, tutorials and stuff, that'd be great. Thanx.
     
  2. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    Game Objects can use ondrag built in, or you can roll your own with interfaces.



    This guy is brilliant I regularly watch his channel

    You need a persistent state object, I'm on my tablet in bed writing this so I can not post at mo but I will if you get stuck with it.

    I don't mind helping anybody that wants to learn.
     
    YoYoYoYo_111_PiPi likes this.
  3. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    Ok, I'll check out the video, and get back to you...
     
  4. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    This is very useful for moving an object within the scene. But what my (bigger) concern is, when we are moving between scenes, to save the data if we made some change in one of them.

    So let's say we have a scene 'Outside of the building' and the scene 'Inside of the building', and we can move freely between the two back and forth... but if we made any changes in some of those scenes, that those changes remain, i.e. scene doesn't restarts itself to it's original state every time we revisit it. For example, games like Monkey Island, you have a lots of different locations (scenes), that you can move around as much as you want. There are objects in them that you can pick up and add to your inventory. Some of them you don't notice right away, but only later in the game after you revisited particular scene for a hundredth time. And then you can make a change in one scene with the object from another, even if you are returning back to your starting location. Standard point and click adventure.

    Unity, it seems, has no connection between the scenes, but treats them as separate objects with their own set of rules. So if something happens in the Scene No.3, it seems you can't affect Scene No.1 with that... if you set an ability to return to the Scene No.1, of course.

    There are tutorials like these, that only help you pass on your data as you are moving along in a linear direction wit the scenes (from previous to the next one):



    But they are not very good if you want to revisit back some locations/scenes.

    Let's say you have an open world game like Skyrim or something. In order to go from your kingdom to the kingdom where you need to slay your arch rival enemy and burn his castle to the ground you need to change scenes. So you do that, it's part of your task. You go to that other scene and you slay them all, you pillage like crazy... you redesign the whole topology. You've done what you came to do. Now, you go back to the scene to where you came from, and nothing. It doesn't carry the data of the marvelous destruction you just inflicted on your enemies. And back in your enemy's territory, everything is like it was before... like you never been there at all. Scenes, it seems, cannot be changed, or at least it's a very hard thing to do in a context of the whole hierarchy, and I cannot seem to figure out what is the way to deal with that. It'd be really weird that there isn't that option in Unity... to affect scenes globally, in a sense.
     
  5. JayJennings

    JayJennings

    Joined:
    Jun 24, 2013
    Posts:
    184
    (There are many ways to do what you want -- the following is but one way and includes no details, just an overview.)

    When you open a scene the axe in that scene is NOT in the actual scene -- instead, you open the scene and then run through a list of objects that should be in that scene (the scene's inventory) and instantiate them in the correct location.

    When your player picks up the axe you delete the axe from the scene's inventory and add it to the player's inventory so that when you look at what he's carrying you know to instantiate an axe. (Those inventory lists could be loaded/saved from a database, a text file, PlayerPrefs, etc.)

    When your player is in another scene and drops the axe, delete it from the player's inventory and add it to the new scene's inventory. You'll probably also save the new location (x,y,z) of the axe so when you recreate that scene the axe will be instantiated where it was dropped.

    I hope that helps get you onto the right track.

    Jay
     
  6. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    I'm sorry but I don't seem to understand, where would the object (the axe) initially be, if not in the scene/scenes?

    I did run into PlayerPrefs tutorials, and now I am looking more into them to see how they function. I've already used DontDestroyOnLoad for audio file that i wanted to play uninterrupted through certain scenes, so I'll see what all possibilities that offers as well.

    But i didn't understood the location of the objects part...

    EDIT: I don't know what you mean by scene's inventory... shouldn't any object be in the scene if it's part of the 'scene's inventory'.
     
    Last edited: Oct 10, 2016
  7. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    As promissed, pigy always pays his debits(Game Of Thrones).

    Right this is taken from MY Gauntlet game

    ProjectComponet is the Class you want to make available to other scenes.

    ComponentBase is a form of wrapper that basically collects the Global State Object which is ProjectComponent Class

    SceneComponent inherits from ComponentBase, you must inherit because of the abstract on the ComponentBase, usually base classes have base on the end of the name.

    Now download these files and put them in your project, SceneComponent is actually the persistent class but because of the way it's written you can put ALL your data that you want to keep from scene to scene in ProjectComponent.

    Create an Empty GameObject YOU must call it Project as ComponentBase looks for that name.

    Now Create a new script call it what you like and add it to any game object Canvas or Camera for example

    NOW YOU MUST change the following in the script you just created

    : MonoBehaviour to : SceneComponent

    Now in your script you can access the Project Class by using this.Project


    PLEASE note I've left stuff in the ProjectComponent Class this will error in MonoD, or Visuals Studio you can comment this out for now I left it in so you have an idea from accessing the data you want to persist.

    I hope this help, I know it's a bit advanced but do a little research on an abstract class and inheritance etc.
     

    Attached Files:

    YoYoYoYo_111_PiPi likes this.
  8. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    Just thought I can also help you with saving data to a file, if I remember, it's saves a binary file.

    This will help you save your data to internal storage.
     
  9. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    Thanx for all of this, you are very kind. I will download and look into it all, but I apologize if I won't answer right back if I don't get something right (I can't stress that enough, I am still just a newbie)... But, hopefully through the next week, I'll reply back on what you've sent, either here or PM you. Again, very kind of you to share scripts on such short notice. Thanx.
     
  10. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    No problem,

    Yes by all means PM me, I think you(anybody I mean) should help others too many people just take take take, and not give back, we all started somewhere.
     
    YoYoYoYo_111_PiPi likes this.