Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

Fade 2 out of 3 gameobjects on the screen who have the same material 2 Answers

What is an instance of a GameObject? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges