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 MightyJoe1 · Dec 03, 2014 at 10:01 PM · animationprefabcharacterclasshumanoid

how can you make a wrapper class for a character?

I'm trying to make a character class that contains all you need for a humanoid character because a prefab is just not working through different scenes.

I get the error: You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all

I guess I want to know how to script character creation instead of using prefabs and having to set everything in every prefab. If my question is unclear please let me know.

here is an example of creating a character:

 private List<HumanCharacter> _characters = new List<HumanCharacter>();

And here is an example of the class:

public class HumanCharacter : MonoBehaviour {

 public Transform transform;

 private AnimationClip[] _animations;      //set in Unity, the animations each character has 
 private GameObject _myself;

 private HumanCharacter() { } //DVC Not Allowed

 //EVC
 public HumanCharacter(GameObject prefab, Vector3 position, Quaternion rotation, AnimationClip[] Animations)
 {
     _myself = (GameObject)Instantiate(prefab, position, rotation);
     _animations = Animations;

     this.transform = _myself.transform;

     if( !_myself.GetComponent("Animation").ToString().Equals("Animation") )
         _myself.AddComponent("Animation");

     AddAnimations();
 }

...

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
Best Answer

Answer by YoungDeveloper · Dec 03, 2014 at 10:20 PM

Yes, you can't use new for class which inherits from MonoBehaviour. If you want the inspector look, monobehaviour is necessary. But most likely you don't, especially when things get hardcore OOP.

The structure really depends on the project requirements, difficulty and what are your future plans.

This is kind of setup i can image.

 public class NetworkPlayer : MonoBehaviour {
     private Player PLAYER;
 
     //#Methods to handle server requests, your player related
     private void ReceivePlayer(#someData){
         PLAYER = new Player(#someData);
     }
 }
 
 public sealed class Player{
     public Player(#PlayerData){
         this.name = #PlayerData.getName();
         this.INVENTORY = #PlayerData.UnparseInventory();
     }
 
     private readonly string name;
     private string guild;
 
     private readonly Inventory INVENTORY;
 
 }
 
 public sealed class Inventory{
     public Inventory(#Data){
         slots = #Data.Unparse();
     }
 
     private byte WIDTH;
     private byte HEIGTH;
     private readonly BagSlot[] slots;
 }

 //SO ON

Only NetworkPlayer component will be place-able on unity gameobject. If you want to see classes and it's variables from inspector, use [System.Serializable] before class declaration. http://docs.unity3d.com/ScriptReference/Serializable.html

Hopefully, this will give you some ideas of how you can manage it.

Comment
Add comment · Show 2 · 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 MightyJoe1 · Dec 05, 2014 at 05:43 PM 0
Share

I get the general Idea! Did you make your classes sealed to prevent unity from doing something, or is it just your structure?

avatar image YoungDeveloper · Dec 05, 2014 at 05:56 PM 0
Share

You can't inherit from sealed classes. Structure is very important when is comes to security and/or if you develop in API form.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

[3D Game Kit]Change animations from generic to humanoid 4 Answers

A question about unity prefabs and their animations.. 0 Answers

How can I make custom character animation for Humanoid? 1 Answer

I want to animate a prefab I have, and I want to make it a humanoid, how do I do that? 1 Answer

Can skeletal animation be applied to a prefab? 0 Answers


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