i know it may sound trivial, but how the hell do i create an empty gameobject by code without resorcing to prefabs.
var go = new GameObject();
–Eric
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…
thanks for the help.
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!
var go = new GameObject("GO", Rigidbody, AudioSource);
Or use AddComponent.
–Eric
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.
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.
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…
prefabs then? are prefabs always of type Transform?
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
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.
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.
You can look at any GameObject, and see all of the Component scripts attached to it, and change any Variable in them.
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).
@Catsby: I sent you a PM. sounds like you’re overcomplicating things! I’m positive there is an easy solution.
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
In C#
GameObject game = new GameObject();