Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

GameObjects -> Create Other

Discussion in 'Editor & General Support' started by Steffen, Aug 17, 2005.

  1. Steffen

    Steffen

    Joined:
    Aug 11, 2005
    Posts:
    70
    Hi,

    we extends the GameObject class and want to know how we can add this class to the "GameObjects -> Create Other" menu ?

    Ah and a other question is it is possible to use namespaces in c# classes in unity ?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Scripts in C# should only derive from MonoBehaviour.
    Otherwise you can't add them to a game object.

    You can't add menu items to unity from scripting at the moment.
    Instead you can create a prefab with everything setup up and then just drag and drop those prefabs into the right place.
    http://otee.dk/Documentation/Manual/Prefabs.html

    At the moment you can't use namespaces for scripts in C#.
     
  3. Steffen

    Steffen

    Joined:
    Aug 11, 2005
    Posts:
    70
    thx for the fast answer but how can i create a prefab from c# ?
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Do you mean instantiate a prefab from C#?

    What exactly are you trying to do?
     
  5. Steffen

    Steffen

    Joined:
    Aug 11, 2005
    Posts:
    70
    yep.

    We want to build a GameObject that represent a monster. Every monster have the same scripts to interact with the environment/game logic plugin. Now we want a easy way to create this game object in scripts and the editor.

    I thought the best way is to extend the GameObject class but our level-designer cant create the extended gameobjects in the editor :-(
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    The way it normally works is like this:

    1) Create the monster in the scene.
    2) Then create a new prefab in the project view.
    3) Drag the monster onto the prefab in the project view.


    Then you can instantiate the prefab as often as you want in your scenes.
    When you change the prefab, all scenes reflect the change.

    If you want to instantiate at runtime from scripts, use the Instantiate function:
    http://www.otee.dk/Documentation/ScriptReference/Object.html#Instantiate
     
  7. Steffen

    Steffen

    Joined:
    Aug 11, 2005
    Posts:
    70
    but one monster have to be in the scene always or?
     
  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    A prefab is stored in the project folder. Thus it is not in the scene.
     
  9. Steffen

    Steffen

    Joined:
    Aug 11, 2005
    Posts:
    70
    okay.

    1) i created a prefab called "monster1"
    2) i drag "monster1" to the scene
    3) i duplicate "monster" from script
    4) i deleted the "monster1" from the scene
    5) i want to duplicate it again
    6) error null pointer exception with GameObject.FindGameObjectWithTag(...);
    7) i want use GameObject.FindObjectsOfTypeIncludingAssets(...); but i dont know the type of a prefab :-(
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    A prefab to the scripting is just like any other game object which is inactive.

    Now i think you will be much better off if you create a direct link to the monster prefab by adding a public variable in the script you use to instantiate the prefab:
    public GameObject monster;

    Then you assign the monster1 prefab to the monster variable, in the inspector of the object you attached the script to.

    This way you don't need to search for the prefab but you just have a variable and can use and instantiate it right away.
     
  11. Steffen

    Steffen

    Joined:
    Aug 11, 2005
    Posts:
    70
    thx.

    i have a last q for today.

    why i get with this code

    UnityEngine.Object[] a = GameObject.FindObjectsOfTypeIncludingAssets(typeof(UnityEngine.GameObject));
    for(int i=0;i<a.Length;i++) {
    Debug.Log("name: "+a.name);
    }

    only gameobjects that are/wasnt placed on scene and not all gameobjects in the project ?
     
  12. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Because the assets haven't been loaded yet.
    All assets in unity are loaded on demand.

    This is why the documentation warns about using the function.

    The name is misleading i see. I'll fix that.
     
  13. Steffen

    Steffen

    Joined:
    Aug 11, 2005
    Posts:
    70
    new day new questions :)

    how can i load/unload assest from c# ?
     
  14. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    You just make a property that points to it. The first time you use the asset, it gets loaded automatically.

    Unloading happens automatically as well when you load a new scene.
     
  15. Steffen

    Steffen

    Joined:
    Aug 11, 2005
    Posts:
    70
    thx for the very very nice support :)