Search Unity

[Multi Scenes] Move gameobject from scene to scene during edit time

Discussion in 'Scripting' started by xXApOXx, Jul 19, 2017.

  1. xXApOXx

    xXApOXx

    Joined:
    Feb 9, 2015
    Posts:
    76
    Hi,

    I'm currently making a tool to instantiate gameobjects on an empty scene from a template. But i'm stuck when there are sub scenes in my main scene. All objects from the template that should be in the sub scenes are instantiated in the root of the main scene.

    Since the Scene object from EditorSceneManager.GetActiveScene() doesn't expose his transfrom, I can't figured it out. I also can't apply the template one scene by one because the tool features reordering: if it finds the gameobject that it should instantiate, it will automatically replace it instead to avoid duplicates.
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Instantiated objects are always added to the active scene. If you want to add them to a specific scene you should make that scene the active one.
     
  3. xXApOXx

    xXApOXx

    Joined:
    Feb 9, 2015
    Posts:
    76
    Thx it works for the instantiation!

    But as I said, the tool features reordering, if it finds a GO that it should instantiate, it will instead reorder it. But even if the active scene is the right one, gameObjects.transform.SetParent(null); doesn't work. It will set the gameobject on the main scene.

    My solution is to instantiate a copy of the GO and then destroy the original. But maybe I'm doing this wrong?
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Without really understanding what you're trying to do yeah that seems like a fine solution. Otherwise you could put all of your tool's objects in their own scene.
     
  5. xXApOXx

    xXApOXx

    Joined:
    Feb 9, 2015
    Posts:
    76
    The tool's goal is to quickly setup or update all the levels. It's an editor window. The user creates a template where he can add gameobjects or prefabs. For example, our artists need a gameobject container "Scenery" where their own tools will add stuffs for the environment. It's also a way to be sure that all our game managers are present in the scene as well.

    Finally, this solution is a bit risky. The new GO doesn't loose any references but GOs that had reference to the GO that I'm about to destroy and instantiate in the right sub scene, will be lost.

    I'm still looking for better solutions but thanx to you, I'm close ;)
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You might have to roll your own referencing solution.

    I'd still question why you can't just have a "template" scene that you put in any other scene that needs it.
     
  7. xXApOXx

    xXApOXx

    Joined:
    Feb 9, 2015
    Posts:
    76
    The tool has been made after we created some scenes/levels (and we have many to make after) so I want him to be able to fix old scenes.

    I found a way to move a GO from a scene to a subscene without destroying it. Since I can create GO in any scene I want by setting the sub scene active, I create a temp container in which I move my misplaced GO in that temp container and then I set the parent null to be in the right scene!