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 /
avatar image
0
Question by youngapprentice · Apr 22, 2013 at 01:37 AM · javascriptinstantiateprefabclasses

Classes VS Variable Scripts

Hi, all! I make use of classes in some instances of my game and they are very useful. However, I am NOT using classes for my enemies for one simple reason:

I can't seem to figure how to get a new physical enemy prefab to spawn with the constructor and to have that tied to all of its respective class variables.

I am in full knowledge that classes will greatly enhance my game (especially when used with enemies!!)

If someone could point me in the right direction to be able to:

  • Spawn a physical enemy prefab in the constructor (I'm assuming the prefab would be a class variable)

  • Have that physical representation of the enemy be linked to its respective class instance

Thanks! - YA

Comment
Add comment · Show 1
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 youngapprentice · Apr 23, 2013 at 02:30 AM 0
Share

No takers? :D I know this is possible. Otherwise game companies wouldn't be able to spawn such varied enemies sharing similar variable sets. This is practically what classes are meant for!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Apr 23, 2013 at 06:52 AM

You don't have to make classes that standalone - you can make base classes that inherit from MonoBehaviour and then inherit your derived scripts from those. This is happening magically in your scripts anyway, but if you make it explicit then you can start to create your own class hierarchies.

That said - inheritance is only of minor use in Unity as it has a composition model where multiple scripts affect the same object. You can get more varied and robust combinations of enemies by varying the scripts attached to them.

This can be combined with interfaces for added performance (and a reduction of costly SendMessages).

The answer to this question: http://answers.unity3d.com/questions/15458/extending-from-interface-in-js.html talks about the definition of interfaces.

Basically you explicitly define your class rather than getting Unity to automagically create you one:

       class YourScript extends MonoBehaviour implements ISomeInterfaceYouWrote
       {
       }

You can then inherit from your MonoBehaviour base class if it makes sense:

      class AnotherScript extends YourScript
      {
      }
Comment
Add comment · Show 1 · 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 whydoidoit · Apr 23, 2013 at 06:53 AM 0
Share

You shouldn't be trying to create prefabs in class constructors - it's just hard work and fighting the framework

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

12 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

Related Questions

How do you Instantiate a Prefab and call its instance variable? 1 Answer

Assigning a prefab a number suffix at runtime. 1 Answer

Updating a variable on a script in an instanced object 1 Answer

Why is transform.position of a prefab that's been instantiated different than a gameobject created on the scene, when they are in the exact same position? 1 Answer

Instantiate object 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