Search Unity

Create a scene object from script

Discussion in 'Scripting' started by JRavey, May 11, 2012.

  1. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    Here's a question, I'd like to create a scene object from a script by using editor tools.

    Essentially, I am interesting in creating an object, manipulating it as needed, then adding it to the scene.
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,619
    First of all, check out Prefabs. If that kind of thing doesn't suit your needs, start with "GameObject go = new GameObject();" and go from there - everything you need to know is in the scripting documentation.

    You'll also want to look at the "Extending the Editor" part of the documentation.
     
  3. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    Prefabs aren't what I need.

    I just need to procedurally populate a scene in the editor. I would have no trouble doing this at runtime, but I have to do some in-editor stuff to the scene.
     
  4. dodo

    dodo

    Joined:
    Jul 13, 2011
    Posts:
    49
    "Extending the Editor" documentation will help you as angrypenguin suggested. Or maybe you have to be more specific about what you're trying to do, because just populating the scene with some objects is no different in editor if you're able to do it at runtime.
     
  5. TylerPerry

    TylerPerry

    Joined:
    May 29, 2011
    Posts:
    5,577
    Just use instantiate as you would in a normal scene but in a editor window and then as long as you save the scene it will keep it.
     
  6. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    Thanks. I wasn't entirely sure how it worked and wanted confirmation that my thoughts were on target before starting something only to find my approach was completely wrong. I'm comfortable with adding interfaces and menu items, but I figured somebody must have done this before me.