• 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
1
Question by NbthMnk · Sep 12, 2014 at 09:35 PM · addcomponent

using addcomponent for buffs, using variable for class

I'm trying to build a tiny turn-based battle engine and I hit a roadblock adding 'buffs' to my characters. I have the buffs as children of a parent class called 'Status' and am trying to add them to my Character class as components. I had it working just fine once, but I tried to make the function adding the component more generic and now get the error

no appropriate version of 'unityEngine.GameObject.AddComponent' for the argument list (Status) was found

Adding the buff component - contained in and called by my parent skill class:

 function addStatus(user: Character, buff: Status){
     var nStatus = user.gameObject.AddComponent(buff);
 }

this is how I am calling the addStatus, inside a child skill 'shield':

     var buff: shielded;
     super.addStatus(user, buff); 

shielded is a child of the Status class. All of these classes are contained in different .js files.

This same code works when I put a specific 'buff' into the call = addComponent(shielded); So I assume I'm just using the function wrong or I have completely misjudged how to go about this problem.

If you need to know anything more specific to see what is going wrong, please let me know. Thank you,

Comment
Add comment · Show 2
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 rutter · Sep 12, 2014 at 09:44 PM 0
Share

The syntax you're using suggests that buff is an object reference. AddComponent doesn't want an object, though, it wants a type.

You can ask an object what its type is, for example buff.GetType(), but I don't have a Unity install handy to see how well that works with AddComponent.

Do you really need buffs to be components? Depending on what you need them to do, it might be easier to work with if they're objects that are managed by a component (like a "Buff$$anonymous$$anager", say).

avatar image orb · Sep 12, 2014 at 10:03 PM 0
Share

A buff manager could be a good idea, but certain temporary effects might as well manage themselves too. In a turn-based game I don't think that would be too much work for the system.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by orb · Sep 12, 2014 at 10:00 PM

You might better off making the argument to AddComponent() a string or using typeof(class). You could make an enum named Statuses and make it the second argument to addStatus(). Pseudocode:

 enum Statuses
 {
     Shielded,
     Regeneration
 }
     …
 
 function AddStatus(user : Character, type : Statuses)
 {
     switch(type)
     {
     case Statuses.Shielded:
     user.AddComponent(Shielded);
         …
     }
 }

If all the components are added directly to the character, the variable gameObject already refers to it. So you shouldn't really need the "user" and "super" variables either. It could then be simplified just to AddStatus(type : Statuses).

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 NbthMnk · Sep 13, 2014 at 12:00 AM 0
Share

I'll mess around with this idea. The idea of the character and why I wanted things to be so generic was to allow for different attacks to add and remove buffs. So a character can use a skill that adds a shield to themselves but also adds a 'debuff' to their enemy as part of the same turn. I appreciate the answer. Combining this with the comment above of a 'buff manager' might end up being the way I go.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Trying to child multiple GameObjects to one master object in script 1 Answer

How Do I Get a Script to Reference the GameObject When Using AddComponent? 1 Answer

Creating prefab through custom editor window 1 Answer

AddComponent adds too many (C#) 3 Answers

You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent() 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