Search Unity

Question Trying to make an enemy kill the player

Discussion in 'Editor & General Support' started by SarcoRonin, Dec 7, 2022.

  1. SarcoRonin

    SarcoRonin

    Joined:
    Nov 23, 2022
    Posts:
    9
    Hello, I'm working on a game that has an enemy that supposed to kill you when you touch it. I've tried any and all tutorials I could find but none of them are working for me for some reason, and none of the solutions I've found on similiar issues work. The one I'm using now has one error saying it needs an identifier on line 75, character 36, so I put something and then it says no errors but the character is not being destroyed.


    Code (CSharp):
    1. void OnCollisionEnter(Collision)
    2. {
    3. [INDENT]if (col.gameObject.tag == "Enemy")
    4. {
    5. Destroy(col.gameObject);
    6. }[/INDENT]
    7. }
    The line that says 1 here is the line that's 75 in the coding (I didn't include the other coding because it's unrelated to the specific action that's supposed to happen and has no issues) and character 36 according to VS is when I put the blinking line after the "n" in the word "(Collision)"

    This as presented acutally creates errors in the coding according to Unity, if I add a "col" after the "Collision" so that it reads "(Collision col)" it gets rid of the error saying it needs an identifier, but then the enemy runs face first into me, but the character object is just being pushed, not destroyed. Any help?
     
    Last edited: Dec 7, 2022
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,930
    You seem to be getting typos mixed up with logic bugs, while not exactly understanding the requirements of the C# language. All code needs to have zero syntax errors before Unity can attempt to run your game. If you're unsure, do some tutorials to familiarise yourself with the language as you won't get far otherwise.

    Make sure you are fulfil all the requirements of OnCollisioEnter: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter.html

    Otherwise use Debug.Log to see what values you are getting, and if the code is executing at all.
     
    Kurt-Dekker likes this.
  3. SarcoRonin

    SarcoRonin

    Joined:
    Nov 23, 2022
    Posts:
    9
    Yeah, I'm definitely slowly realizing that I'm biting off a bit more than I can chew here, but I've already made some decisions beforehand and am on a time limit to get a game of some sort out. It's a good thing I'm still relatively early on, I can actually make some time for the learning of C# a bit more before shooting myself in the foot anymore.

    As for the code, I was just following the tutorials that I had found on youtube, so it probably wasn't the best one out there. Thanks for the link, it'll come in handy I'm sure.