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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to create empty game object? [Question]

Discussion in 'Scripting' started by Jeffom, Dec 23, 2010.

  1. Jeffom

    Jeffom

    Joined:
    Oct 6, 2010
    Posts:
    55
    i know it may sound trivial, but how the hell do i create an empty gameobject by code without resorcing to prefabs.
     
    Last edited: Dec 23, 2010
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. var go = new GameObject();
    --Eric
     
  3. devans770

    devans770

    Joined:
    Nov 25, 2010
    Posts:
    45
    haven't tried it but I would assume as it is just a class that it would be something like (JS):

    var mygameobject : GameObject;
    mygameobject = new GameObject();

    or (C#):

    GameObject mygameobject = new GameObject();

    you could then add components or whatever there after..
     
    BorovinskayaLA likes this.
  4. Jeffom

    Jeffom

    Joined:
    Oct 6, 2010
    Posts:
    55
    thanks for the help.
     
  5. Catsby

    Catsby

    Joined:
    Nov 16, 2010
    Posts:
    37
    I've been trying to find a way to solve a problem I'm having with character party creation and I think this might be the way to do it. Once you've created a new game object how do you add components?

    I was about to start a new thread with a wall of text too!
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. var go = new GameObject("GO", Rigidbody, AudioSource);
    Or use AddComponent.

    --Eric
     
    mrtkhosravi likes this.
  7. Jeffom

    Jeffom

    Joined:
    Oct 6, 2010
    Posts:
    55
    simple get the object , object.AddComponent<nameofcomponent() in c# and in js i think its just object.AddComponent(nameofcomponent).
    if not, tell more about your problem.
     
  8. Catsby

    Catsby

    Joined:
    Nov 16, 2010
    Posts:
    37
    I haven't tried doing it by making new game objects yet but this is what I'm trying to do;

    I made a little GUI based game when I was learning how to use JS but as it is it doesn't utilize unity at all. It could probably run in any JS compiler if there were similar GUI classes. Anyways, I want to try to move it into unity using game objects and provide different functionality but I couldn't find tutorials with examples similar to what I'm trying to do. Basically, I have 3 character scripts (for 3 separate player character's info/parameters), a manager script (for the update function) and an equations script (for almost all the functions) for a fight scenario similar to an (really) old school RPG.

    By moving what I have into game objects I want to be able to allow the player to have any assortment of the 3 characters via some sort of selection in game. I figure by creating new game objects I could create 3 new game objects for each party slot, gut the GUI information from each of the previous character scripts and place them into 3 different object scripts and then add a character info and GUI scripts to each of the 3 new game objects, depending upon the player's selection though.

    There has to be a simpler way to do this though since I can't find any examples or tutorials that do anything like this anywhere.
     
  9. KrankyBoy

    KrankyBoy

    Joined:
    Mar 27, 2009
    Posts:
    737
    As an empty game object is almost useless, why would you want to do that? Makes more sense to create prefabs that already contain all the components you want/need and instantiate those...
     
  10. Catsby

    Catsby

    Joined:
    Nov 16, 2010
    Posts:
    37
    prefabs then? are prefabs always of type Transform?
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's not feasible if you are creating dynamic game objects at runtime. There's a reason why that functionality exists.

    Prefabs have no inherent type.

    --Eric
     
  12. callahan.44

    callahan.44

    Joined:
    Jan 9, 2010
    Posts:
    694
    I'm having a really hard time trying to understand exactly what you're after :)

    It's perfectly possible to create a new character from a new GameObject, and then add a mesh/texture, an inventory, a stats/skill system and AI/controller behaviour script to it, all selected in another part of your game.

    One Update() is pretty limiting, I'd expect the AI script to have its own, etc. If each character had a breathing animation, it would be crazy to have a Manager tell each of the characters in turn to breathe. Better to let them get on with it themselves. You will have managers but they'll be handling specific things. The trivial stuff will be done in a local Update()

    GUI is just for use with a camera, so not sure what Info you think it needs? The GUI is just a "window" to look at specific game data and have the ability to alter it.
     
  13. Catsby

    Catsby

    Joined:
    Nov 16, 2010
    Posts:
    37
    Arg. Have no idea how to do what I want to do then. I need to be able to access variables and functions in a script on a game object without knowing what the script is named since the player is choosing which characters they want to use.

    edit: forgot to mention that the variables and functions are similar in the scripts.
     
    Last edited: Dec 23, 2010
  14. callahan.44

    callahan.44

    Joined:
    Jan 9, 2010
    Posts:
    694
    You can look at any GameObject, and see all of the Component scripts attached to it, and change any Variable in them.
     
  15. Catsby

    Catsby

    Joined:
    Nov 16, 2010
    Posts:
    37
    Do you mean in unity inspector view? I can see how you do it there but not in JS.

    Right now I'm trying 3 separate game objects for the 3 party slots. Each party object will have a GUI information script attached that will access variables from prefabs (for player characters). The problem is that I don't know how to make a variable for the prefab or manipulate variables in a script without knowing the script's name.

    In all the examples I can find you change variables/access functions between scripts by accessing them like this; script.variable/function();

    but if you don't know the prefab or script name yet, since the player has to chose which characters they want to use, how would you access the variables and functions? ( variables/functions for hp, mana, whatever are declared similarly in all character scripts).
     
  16. callahan.44

    callahan.44

    Joined:
    Jan 9, 2010
    Posts:
    694
    @Catsby: I sent you a PM. sounds like you're overcomplicating things! I'm positive there is an easy solution.
     
  17. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,541
    That sounds like you need a design pattern. ;)
    If you have 3 scripts that are designed to do different things, but every script provide some similar variables or functions you should use class inheritance.
    All common variables and functions should be declared in the one base class.

    Like FizixMan told you already in this >>post<<
    The second code block shows how you should design your 3 classes and the base class of course.

    As far as i know you can use GetComponent(BaseClassName) to get even scripts that are inherited from this base class.
    I'm not 100% sure if that works in JS (i don't use this "dirty" language ;) ) but it definitely work in C#

    That's the clue of inheritance:
    Every class have it's own type and typename. To access such a class you need to know the exact type of the class. But if a class inherits from a base class the new class have 2 types. The own classtype and the type of the base class. That way you have 3 different classes but all have also a common type: the base class.

    That's all absolute fundamental knowledge in object-oriented-programming. If you aim for more complex code and behaviours it's important to gain basic knowledge of your preferred language.

    good luck
     
  18. wasifmac

    wasifmac

    Joined:
    Feb 4, 2017
    Posts:
    4
    In C#

    GameObject game = new GameObject();
     
    QuincyC likes this.