Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 08, 2016 at 02:36 PM · c#unity 5scripting problemroguelike

unable to attack enemy objects?

I've been working in the 2d roguelike tutorial, and after finishing it, I'm going through and adding some more features to it to make it my own thing, and learn more about coding.

I'm trying to add a feature to allow the player to attack the enemy characters, which is set up the same as both of the functions for attacking walls, and enemies attacking the player, but when the character moves towards an enemy space, he doesn't attack at all.

The only difference is that I've set up an item that the player must collect on each level before he can attack enemies, which is just a boolean, called "hasAxe" I've set to public temporarily so I can verify that it's triggering correctly in the inspector.

Does anyone know why this is failing? there's no runtime error in the console.

attack function in the player script:

 if(component.GetType() == typeof(Enemy))
         {
             
             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 healthpoints to be subtracted.
                 hitEnemy.LoseHealth(axeDamage);
 
                 //play the chop sound
                 SoundManager.instance.RandomizeSfx (chopSound1, chopSound2);
 
                 //Play the axe animation
                 animator.SetTrigger ("playerAxe");    
             }
         }


Enemy Damage code:

     public void LoseHealth (int loss)
     {
         //Subtract lost food points from the players total.
         enemyHealth -= loss;
 
         CheckForDeath();
     }
     private void CheckForDeath()
     {
         if (enemyHealth <=0)
         {
             gameObject.SetActive (false);
         }
     }

at this point, the code gets a little long to feel comfortable posting all of it to display on a webpage, so I'm going to throw the scripts in pastebin.

Player Script: http://pastebin.com/ABKd18vC

Enemy Script: http://pastebin.com/zeDXVHu4

Wall Script: http://pastebin.com/PNXkvAbw

MovingObject(parent of Player and Enemy): http://pastebin.com/mXR8U7uX

Comment
Add comment · Show 3
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 Brocccoli · Apr 08, 2016 at 03:57 PM 0
Share

Just a quick comment. You're only calling the attack logic in OnCant$$anonymous$$ove<T>. I think this may be your problem as the original logic seems to be implemented to attack certain walls, so it may not even be called when attempting to attack and enemy.

avatar image saintshenanigans Brocccoli · Apr 08, 2016 at 04:21 PM 0
Share

Sorry, but I'm confused.. I'm fairly new to coding. Where else would I need to call it? I have the function set up in Enemy the same way it should be for Player and Wall

     public void LoseHealth (int loss)
     {
         //Subtract lost food points from the players total.
         enemyHealth -= loss;
 
         CheckForDeath();
     }
avatar image saintshenanigans · Apr 10, 2016 at 12:53 AM 0
Share

Bump.. Can anyone help, please? I'm stuck at a standstill until I can figure this out :(

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DGH1988 · Aug 24, 2017 at 07:55 AM

Hi there,

I am struggling with a similar issue. I tried copying as much of the system used in the wall script but can't get it to work. What I did was assign a second script to the Enemy1 and 2 prefab which is basically a copy of the wallscript. Everything in the player script and the copy of the wall script (assigned to the enemy1 and 2) of course has unique names usually just by replacing the word "wall" with "enemy". I also tried adding the concept used in wall script to the enemy script but that also didn't work. Have you found a solution yet? Of course I can elaborate further if requested.

Cheers,

Daan

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 DGH1988 · Aug 24, 2017 at 08:24 AM 0
Share

Boom! I got it!

I replaced the wall script with a script that contains both the wall and the enemy being damaged and removed, of course amended the player script as well a little bit.

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

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

Location Does Not Change 1 Answer

Dynamic script loading? 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