Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 RemDust · Jun 21, 2017 at 12:02 PM · scripting beginnermethodsskills

How to trigger a "generic action" that can call different methods ?

Hi guys ! How can I manage to call different methods with same button ?

I mean, Fire2 should trigger the "active skill" but skills can have a very different behaviour ?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by FlaSh-G · Jun 21, 2017 at 12:40 PM

There are lots of posible answers to this. The best solution highly depends on what you are trying to generally accomplish, and on your approach to get there. And every possible answer will bring with it quite a few things to research in order to get going with it.

Start off with defining what a "skill" is, from a technical point of view. You somehow have to teach Unity what a skill is so you can define that there's multiple of them and one of them is "active". One solution that is pretty fancy (and not as confusing as it used to be in the past) are ScriptableObjects. A ScriptableObject is basically™ an asset that you program yourself.

Here's an example:

 using UnityEngine;
 
 [CreateAssetMenu(fileName = "Skill", menuName = "ScriptableObject/Skill")]
 public class Skill : ScriptableObject
 {
     public int damage;
 }

As you can see, this is not a MonoBehaviour and thus not a component you can slap on a GameObject. Instead, thanks to the attribute on top of the class, you can right-click in your project view, hit Create, ScriptableObject, Skill. This will create a new skill in your assets, just like you can create a new C# script there.

When you click your skill, you can rename it and set values for its properties in the inspector. The rules that apply are the same for properties in MonoBehaviour: All the public variables are exposed in the editor and saved in the project.

Next, you can create a regular MonoBehaviour script that utilizes skills:

 using UnityEngine;
 
 public class PlayerSkills : MonoBehaviour
 {
   // An array of skills
   public Skill[] skills;
   public int activeSkillIndex;
 
   void Update()
   {
     if(Input.GetButtonDown("Fire 1"))
     {
       var skill = skills[activeSkillIndex];
       Debug.Log(skill.name + " deals " + skill.damage + " damage.");
     }
   }
 }

Note how the damage value that has been defined in the ScriptableObject class and got a value from the editor can easily be accessed here. You can now change "Active Skill Index" in the editor and see how pressing the button triggers a different skill each time (assuming you have more than one skill created and added to the list in the component).

Remember that this is one possible solution. Depending on what you are doing and how you are doing it, it might not be the perfect one for you.

Comment
Add comment · 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
0

Answer by Bunny83 · Jun 21, 2017 at 12:47 PM

Just use classic OOP. Either define a base class from which each skill class is derived or define an interface which each skill class implements. An interface is usually the better approach:

 public interface ISkill
 {
     void TriggerAction();
 }
 
 public class SomeSkill : MonoBehaviour, ISkill
 {
     public void TriggerAction()
     {
         // Trigger this skill's action
     }
 }
 
 public class AnotherSkill : MonoBehaviour, ISkill
 {
     public void TriggerAction()
     {
         // Trigger this skill's action
     }
 }

Now you can store your "current skill" in a variable of type "ISkill". You can simply do

 ISkill currentSkill;
 
 void TriggerSkillAction()
 {
     if (currentSkill != null)
         currentSkill.TriggerAction();
 }

You can assign any class that implements the ISkill interface to the "currentSkill" variable.

Comment
Add comment · 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

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

70 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 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 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

Simply calling a method from another script :( 1 Answer

Make method variable show up in Inspector 1 Answer

Optional parameter, editor raises error "Unexpected symbol `=' " 0 Answers

Generalized call of specific methods 1 Answer

Problem with making skill system for my creatures 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