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 /
  • Help Room /
avatar image
0
Question by saintshenanigans · Apr 06, 2016 at 07:55 PM · c#unity 5scripting problem

use a function twice

I'm working in the roguelike tutorial, and trying to edit the code and game to make it my own, and I'm having a little trouble.

I have a script called MovingObject as a parent of my player and enemy scripts.

I have a function in there called OnCantMove

 protected abstract void OnCantMove <T> (T component)
             where T : Component;

I have it again in player, but I need to use it twice. once for attacking enemies, and once for breaking walls. Does anyone know how to do this?

 protected override void OnCantMove <T> (T component)
     {
         //Set hitWall to equal the component passed in as a parameter.
         Wall hitWall = component as Wall;
 
         //Call the DamageWall function of the Wall we are hitting.
         hitWall.DamageWall (wallDamage);
 
         if (hasAxe == false)
         {
             //Set the attack trigger of the player's animation controller in order to play the player's attack animation.
             animator.SetTrigger ("playerChop");
         }
 
         else if (hasAxe == true)
         {
             //Set the attack trigger of the player's animation controller in order to play the player's attack animation.
             animator.SetTrigger ("playerAxe");
 
         }
 
     }



attack enemy:

 protected override void OnCantMove <T> (T component)
     {
             if (hasAxe == false)
             {
                 return;
             }
             else if (hasAxe ==true)
             {
 
                 //Declare hitEnemy and set it to equal the encountered component.
                 Enemy hitEnemy = component as Enemy;
 
                 //Call the EnemyHit function of hitEnemy passing it axeDamage, the amount of foodpoints to be subtracted.
                 hitEnemy.EnemyHit (axeDamage);
 
                 //play the chop sound
                 SoundManager.instance.RandomizeSfx (chopSound1, chopSound2);
 
                 //Play the axe animation
                 animator.SetTrigger ("playerAxe");    
             }
         }



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 Ckboyd89 · Apr 06, 2016 at 11:44 PM

You can use the GetType() method to determine which component was passed. So it would look something like the following:

 protected override void OnCantMove<T>(T component)
 {
      if(component.GetType() == typeof(Enemy))
      {
           Enemy hitEnemy = component as Enemy;
           // Do enemy hit logic
      }
      else if(component.GetType() == typeof(Wall))
      {
           Wall hitWall = component as Wall;
           // Do wall hit logic
      }
 }

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 saintshenanigans · Apr 07, 2016 at 02:24 PM 0
Share

this worked perfectly! thanks!

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

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

It does not work removing items from the inventory 0 Answers

Rayscast from the GameObject that is not the player does not work 0 Answers

Error `UnityEngine.Component' does not contain a definition 1 Answer

[uNet] Multiple Spawnpoints with Selection 0 Answers

How to check if a player has passed a certain position?? 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