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();
}
}
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>();
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
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
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);
}
}
Do you have a class (C# script) called Player that is added to the Player GameObject as a component?
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'
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
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?)