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

Need help in Creating Game Object by codes

Discussion in 'Scripting' started by birdkingz, Jul 25, 2011.

  1. birdkingz

    birdkingz

    Joined:
    Jun 6, 2011
    Posts:
    8
    Hi, I know how to create game object by codes but can I create a GameObject which only available in Editor but not during PlayMode?

    What I mean is let say in Editor I've created a GameObject - A through codes.
    After that once I click on Play, the A will automatic destroyed.

    Is it possible to do something like this in Unity?
     
  2. flamy

    flamy

    Joined:
    Feb 7, 2011
    Posts:
    194
    if u totally dont need that object in game use the following script in a file and attach to it...

    function Start()
    {
    Destroy(gameObject);
    }



    if u need the object for some calculation and dont want that to be shown in the game use

    function Start()
    {
    transform.GetComponent("MeshRenderer").enabled=false;
    }
    }
     
  3. oneXwork

    oneXwork

    Joined:
    Mar 3, 2011
    Posts:
    266
    you can also untick the gameObj
    using active
     
  4. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    What is your end goal with this? Seems a bit silly to have the runtime first load the GameObject and then spend more work on immediately tearing it down.
     
  5. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Heh, not every answer is "go find it". In this case... he posted correctly though I don't think anyone has explained to him the usefulness of Prefabs.

    Create a prefab of your object, In your code when you define a member like a Transform or GameObject, drag the prefab to it, instead of something in the scene. This way, you can create and destroy objects as you need and they are really never in the scene to start.

    This is a great method to do bullets and other objects which really don't exist in the scene until they are ready.