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 ericsjo119 · May 11, 2020 at 10:48 AM · error messagedamageenemy damageplayer damagehealth

Unity: Player take damage from enemy projectile

Here is a snippet of the code I have and this code is for making the player take damage when a projectile hits him but when I enter the code I get an error saying: error CS0246: The type or namespace name 'Player' could not be found (are you missing a using directive or an assembly reference?)

I have given the player object the Player tag and I have also named the object Player but nothing seems to be working.

Does anyone know how to fix this? I believe it should be an easy fix but I guess I am too big of a noob haha.

       void OnTriggerEnter2D(Collider2D hitInfo) {
               if(hitInfo.CompareTag("Player"))
               {
                   Player player = GameObject.FindGameObjectWithTag("Player");
                   player.TakeDamage(20);
                   DestroyProjectile();
               }
           }
Comment
Add comment · Show 1
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 DrewDunne · May 12, 2020 at 09:51 PM 0
Share

Not sure if you figured this out - but the issue is with the following line of Code:

 Player player = GameObject.FindGameObjectWithTag("Player");

This code simply assigns the Game Object to 'player', which does not have the method you are looking for (TakeDamage). Ins$$anonymous$$d, you want the script component attached to the Game Object with tag "Player" by adding '.GetComponent()'. An example based on your comments below might look like:

 Player$$anonymous$$ovement player = GameObject.FindObjectWithTag("Player").GetComponent<Player$$anonymous$$ovement>();





2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DrewDunne · May 11, 2020 at 08:08 PM

Hi @ericsjo119

Nice try with the GameObject change, I think you spotted where the problem was but your solution was just a little off. (To see a list of methods available to GameObject, you can click anywhere on it and hit F12. This will take you to GameObject.cs - and since there is no TakeDamage method in it's class, you cannot make a GameObject take damage while the object is of type GameObject)

Can you share a code snippet of the class 'Player' being defined in your project? If you don't know what that means, or you don't have that anywhere, can you instead tell me what class the code you shared is contained in? You should see it at the top of the document.

Also, where is 'TakeDamage' method? Is it in the same class as this 'voidOnTriggerEnter2D' method?

Cheers!

  • Andrew

Comment
Add comment · Show 5 · 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 ericsjo119 · May 12, 2020 at 09:55 AM 0
Share

Hello Andrew. Thank you for your respond. The snippet of code is contained in the projectile script and the 'TakeDamage' method is the 'Player' and 'Enemy' class since I am using it for both the player and the enemy, I don't know if I could just put it in one place and use it from there but yeah that's how it is right now al$$anonymous$$st.

And both of the classes are using $$anonymous$$onoBehaviour, if that has to do with anything.

Hope this will help you so you can help me :D

avatar image ericsjo119 · May 12, 2020 at 10:00 AM 0
Share

Btw I forgot to add that I have almost the same script for an enemy and that works just fine. And the code down below is in the enemy class and the TakeDamage method is still in the 'Player' and 'Enemy' class.
Here is that code.

 void OnTriggerEnter2D(Collider2D hitInfo) 
     {
         Enemy enemy = hitInfo.GetComponent<Enemy>(); 
         if (enemy != null)
         {
             enemy.TakeDamage(damage);
         }
     }

avatar image B3Designs · May 12, 2020 at 02:12 PM 0
Share

Do you have a class (C# script) called Player that is added to the Player GameObject as a component?

avatar image ericsjo119 B3Designs · May 12, 2020 at 03:55 PM 0
Share

Thank you for your help I know how to fix the problem now so I have a script called 'Player$$anonymous$$ovement' and inside of it I have my TakeDamage method and I now realized that I need to change the 'Player player' to 'Player$$anonymous$$ovement player' but when I do I get this error Cannot implicitly convert type 'UnityEngine.GameObject' to 'Player$$anonymous$$ovement'

avatar image B3Designs ericsjo119 · May 13, 2020 at 10:01 AM 0
Share
 Player$$anonymous$$ovement player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player$$anonymous$$ovement>();

You are finding the GameObject, then you need to get the Player$$anonymous$$ovement component from it (as above).

That should fix it...

Danny

avatar image
0

Answer by ericsjo119 · May 11, 2020 at 12:18 PM

I also tried to replace Player player (line 4) with GameObject player but then I got this error: GameObject' does not contain a definition for 'TakeDamage' and no accessible extension method 'TakeDamage' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)

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

206 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

playerdamage not working. help fix? 0 Answers

I have a jump scare and I want it to affect my player's health when it activates it and kill my player when health = 0. 0 Answers

Best practice where to store Projectile Damage amounts? 1 Answer

Health and Damage Script Not Working 2 Answers

How to make a zombie(enemy) do constant damage when it touches you? 3 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