#I get this error Assets\Script\Knockback.cs(19,17): error CS0118: 'enemy' is a variable but is used

1 messages · Page 1 of 1 (latest)

gentle sedge
#
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class Knockback : MonoBehaviour

{
    
    [SerializeField] private float thrust;
    public float knockTime;
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("enemy"))
        {
            Rigidbody2D enemy = collision.GetComponent<Rigidbody2D>();
            if (enemy != null)
            {
                enemy isKinematic = false;
                Vector2 difference = enemy.transform.position - transform.position;
                difference = difference.normalized * thrust;
                enemy.AddForce(difference, ForceMode2D.Impulse);
                StartCoroutine(KnockCo(enemy));
            }
            
                
            
        }
    }

    private IEnumerator KnockCo(Rigidbody2D enemy)
    {

        if (enemy != null )
        {
            
            yield return new WaitForSeconds(knockTime);
            enemy.velocity = Vector2.zero;
            enemy.isKinematic = true;
            
        }
    }
}```
#

what site shoudl I use to shorten

#

it is looking corrupted here

tough pollen
#

!code

keen matrixBOT
#
Posting code

📃 Large Code Blocks
Large code blocks should be posted as links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/
https://paste.myst.rs/, https://hastebin.com/

📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To get C# formatting the first line should only contain cs or csharp.
Add a comment with a line number if there is an error message.
```cs
// Your code here
```
Do not share screenshots of code unless requested.

gentle sedge
tough pollen
#

enemy isKinematic = false;

gentle sedge
#

oh thanks