Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Creating gameobject at runtime and dragging it to prefabs

Discussion in 'Scripting' started by MarsaPalas, Dec 13, 2014.

  1. MarsaPalas

    MarsaPalas

    Joined:
    Oct 28, 2014
    Posts:
    17
    I have a script that instantiates lot of prefabs at runtime. It also sets some random prefab's properties. I need that bunch of instantiated prefabs in my latter project, so I drag and drop all that in my Prefabs folder.

    That's where the problem is, all those made gameobjects revert their properties set in the script. How to keep that from happening?
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Instead of dragging them while the game is running.

    Start the game:

    1.) Select all the Game Objects
    2.) Copy them to clip board
    3.) Stop the game
    4.) Paste into your scene
    5.) Drag them into the Projects folder to make them prefab.

    hope this helps
     
  3. MarsaPalas

    MarsaPalas

    Joined:
    Oct 28, 2014
    Posts:
    17
    Thank you for your reply! But my problem was I had some objects created at runtime, so they got destroyed afterwards.
     
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Read what he said again, his solution will work. If you copy the objects while the game is running and then stop the game, you can paste the objects as they existed during runtime into the heirarchy view. From there you can put them in the prefabs folded.
     
  5. MarsaPalas

    MarsaPalas

    Joined:
    Oct 28, 2014
    Posts:
    17
    I've tried it too, and it didn't work for me. Sprites that were created during the runtime were destroyed after I saved the gameobject in that way.

    I used (Texture2D)Resource.Load("someTexture") and created Sprite using the Texture2D. And that way didn't work.
    But after I loaded texture as Sprite, it did work.