• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by teomaragakis · Sep 22, 2014 at 05:01 PM · gameobjectinstanceclasses

How to add items (or players) to the scene from classes?

Ok, I am a bit confused, and what I have read until now does not help a lot (but maybe I'm not asking right). I am learning Unity by creating a dungeon crawling RPG. It uses classes (and extensions) for mobs, players, items, everything. Now we have some of the following classes:

Item:MonoBehaviour -> Equipable :Item -> Weapon:Equipable

Similarly we have Character:MonoBehaviour -> Player:Character, Npc:Character

Now I got these classes all filled in with variables and everything. The Player class adds a n input movement controller while the Npc one adds AI, Items have sprites etc. I am even using generic empty prefabs for players and items to simplify some parts.

Now my problem is, when it's time to add all that stuff to the game (as game objects), what's the best way to do it? I cannot obviously do it in the constructor, because it returns an object from that class, not a game object. Do I add a "spawn" function for players for instance, which does that? When a player drops an item from an inventory, do I do the same for that?

I don't want a lot of objects in my game scene, so I like to keep stuff in arrays and use physical (game object) representations when I must. Can anyone guide me here?

Edit: To make this clearer:

A class is a blueprint for an object and the constructor is how the object is built. I want to use this logic for game objects. Can / should I?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by 767_2 · Sep 22, 2014 at 05:10 PM

why dont you use Instantiate to make gameObject out of prefabs and if you want it more robust you should use object Pooling here is tutorial

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image teomaragakis · Sep 22, 2014 at 05:14 PM 0
Share

Because I'm not only trying to learn Unity, but I'm also trying to improve my OOP skills. And I like the create from code approach, my prefab should be my class. Prefabs are there to make our lives easier, but I want to learn it the "proper" way.

avatar image 767_2 · Sep 22, 2014 at 05:20 PM 0
Share

you mean you want to create an object without instantiating it

avatar image tw1st3d · Sep 22, 2014 at 05:29 PM 0
Share
 this.createObject(gameObject, Vector3, Quaternion);
 
 void createObject(obj, pos, rot) {
     Instantiate(obj, pos, rot);
 }

Probably totally different from what you want, but if you want a custom call for the "Instantiate", do something like this.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access an instance of a class when the class isn't known 2 Answers

Add an instance of a script as a component? 1 Answer

Why does my class script not recognize variables in its own instance? 1 Answer

Affect All Instances Of A Prefab 3 Answers

C# Object reference not set to an instance of an object right after GameObject.Instantiate 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges