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. Dismiss Notice

How to copy gameobjects in script(like ctrl+c -> ctrl +v)

Discussion in 'Scripting' started by Loewi_f, May 11, 2021.

  1. Loewi_f

    Loewi_f

    Joined:
    Nov 13, 2019
    Posts:
    30
    I want to delete some gameobjects OnSavingScene,but recover them OnSavedScene.
    I know that EditorSceneManager.sceneSaved and EditorSceneManager.sceneSaving is the very time to do it, but I can't reference to these gameobjects ,because when I copy them and destory them on EditorSceneManager.sceneSaving ., the reference will be null.So, on EditorSceneManager.sceneSaved,I can't instantiate them . I think that some function like ctrl+c -> ctrl +v will help ,but have no idea to do it.Does anyone have some suggestion for this issue?
     
  2. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    ctrl+d creates a duplicate, then you move it where you want.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,195
    You could try to actually ctrl+c/ctrl+v:

    Code (csharp):
    1. // copy
    2. Selection.objects = yourGameObjects;
    3. EditorApplication.ExecuteMenuItem("Edit/Copy");
    4. // paste
    5. EditorApplication.ExecuteMenuItem("Edit/Paste");
    I haven't tried it myself, though.

    An alternative is to set the hide flags to DontSave in sceneSaving, and then set them back again to None in sceneSaved.
     
  4. Loewi_f

    Loewi_f

    Joined:
    Nov 13, 2019
    Posts:
    30
    Oh,thanks a lot. Hide flags seem to be a more smart method.