Sign in to follow this  
way2lazy2care

Classes in Unity - More noob @ unity help

Recommended Posts

Can someone explain how classes work in unity? I am trying to create an array of custom unity game objects. I'm using C#, and I'm not sure if things I create in the unity project (like prefabs and stuff) are considered classes or how I should refer to them in my code.

Any help is appreciated.

Share this post


Link to post
Share on other sites
Quote:
Original post by way2lazy2care
Can someone explain how classes work in unity? I am trying to create an array of custom unity game objects. I'm using C#, and I'm not sure if things I create in the unity project (like prefabs and stuff) are considered classes or how I should refer to them in my code.
Fundamentally, C# classes in Unity are just like classes in any other C# program, so the first step will be to make sure you're thinking of them as such. You can create custom classes (or structs), include them as members in other classes (or structs), use inheritance and polymorphism, etc., just like you would normally.

Now, beyond that, Unity has its own API that it exposes, which has various classes and structs of its own. When writing your own code, you'll often interact with these classes and structs.

The basic structure of a Unity scene is that there are game objects, game objects can have children (through which you can create hierarchies of game objects), and game objects can have components attached to them. An 'empty' game object has only a transform component and nothing else; you can then add additional components to customize the appearance and behavior of the game object. (You may already know all this.)

On the C# side, a game object is represented by an instance of the GameObject class. When you create a custom script component, you inherit (directly or indirectly) from MonoBehaviour, so that's essentially the base class for custom components.

Prefabs are just 'data packages' that describe how to build a game object or hierarchy of game objects; there's no 'Prefab' class that I'm aware of.

There are various ways to create game objects or game object hierarchies, and to acquire references to existing objects or prefabs, both in the editor and via code. If you can describe exactly what it is you're trying to do, it should be pretty easy to point you towards some specific references or examples.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
There are various ways to create game objects or game object hierarchies, and to acquire references to existing objects or prefabs, both in the editor and via code. If you can describe exactly what it is you're trying to do, it should be pretty easy to point you towards some specific references or examples.


I'm trying to make an array of "Player" gameobjects in my code. So I have a player prefab with a PlayerScript (the class of the "PlayerScript" is the "PlayerScript" class, not Player). I am wondering if I need to refer to the array I am making as an array of "Player" objects, and if I need to import them in some special way, or if it will automatically load stuff in the hierarchy.

Side question though, is pretty much everything in the hierarchy accessible as some sort of C# class (and more specifically as the class with the name of whatever it is in the project?)? That would clear up a lot of questions for me I think.

Share this post


Link to post
Share on other sites
Quote:
I'm trying to make an array of "Player" gameobjects in my code. So I have a player prefab with a PlayerScript (the class of the "PlayerScript" is the "PlayerScript" class, not Player). I am wondering if I need to refer to the array I am making as an array of "Player" objects, and if I need to import them in some special way, or if it will automatically load stuff in the hierarchy.
That you have a prefab called 'Player' doesn't mean there's a class called Player. You could create an array of GameObject's and populate it with references to objects cloned from the 'Player' prefab. You could also store only the PlayerScript components, if that's what you'll be interacting with primarily.

Also, if you're not already familiar with it, you'll want to take a look at the Instantiate() function.

In any case, there's usually more than one way to solve this sort of problem, so again, what approach will be best will depend on the context and on your exact requirements.
Quote:
Side question though, is pretty much everything in the hierarchy accessible as some sort of C# class (and more specifically as the class with the name of whatever it is in the project?)? That would clear up a lot of questions for me I think.
It depends on what you mean by 'the name of whatever it is in the project'. If you have a game object in the hierarchy called 'Knight', that doesn't mean there's a C# class called 'Knight'. The names of the game objects are just string identifiers; they don't correspond to any particular existing type. From that point of view, the 'type' of every object you see in the hierarchy is simply 'GameObject'.

But yes, generally speaking you can interact with (almost) everything you see in the hierarchy via scripting in one way or another.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this