Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

HELP!

Discussion in 'Scripting' started by camlightbody, Nov 24, 2020.

  1. camlightbody

    camlightbody

    Joined:
    Nov 21, 2020
    Posts:
    10
    Hi I am making a top down game and was wondering what is wrong with this script. I am getting 3 errors;

    Assets\Scripts\Knockback.cs(18,17): error CS0103: The name 'enemy' does not exist in the current context,

    Assets\Scripts\Knockback.cs(30,38): error CS0246: The type or namespace name 'PlayerMovement' could not be found (are you missing a using directive or an assembly reference?),

    Assets\Scripts\Knockback.cs(31,40): error CS0246: The type or namespace name 'PlayerMovement' could not be found (are you missing a using directive or an assembly reference?)

    Can I get Some Help.

    (P.S This script is for knocking a player back when they touch a enemy)


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Knockback : MonoBehaviour
    {
    public float thrust;
    public float knockTime;
    private void OnTriggerEnter2D(Collider2D other)
    {
    if(other.gameObject.CompareTag("Enemy") || other.gameObject.CompareTag("Player"))
    {
    Rigidbody2D hit = other.GetComponent<Rigidbody2D>();
    if(hit != null)
    {
    enemy.isKinematic = false;
    Vector2 difference = hit.transform.position - transform.position;
    difference = difference.normalized * thrust;
    hit.AddForce(difference, ForceMode2D.Impulse);
    if (other.gameObject.CompareTag("Enemy"))
    {
    hit.GetComponent<Enemy>().currentState = EnemyState.stagger;
    other.GetComponent<Enemy>().Knock(hit, knockTime);
    }
    if (other.gameObject.CompareTag("Player"))
    {
    hit.GetComponent<PlayerMovement>().currentState = PlayerState.stagger;
    other.GetComponent<PlayerMovement>().Knock(knockTime);
    }


    }
    }
    }

    }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Use CODE tags when posting code on the forums. There's a sticky thread in the scripting forum if you can't figure out how it is done.

    The first error is because you are trying to use a variable which you haven't even declared, so the compiler has no idea what it is. specifically:

    Code (csharp):
    1. enemy.isKinematic = false;
    What is "enemy"? You have to declare it somewhere before you can use it, so that the compiler knows what "enemy" is.

    On the PlayerMovement errors, it is saying you have no script called PlayerMovement. You might have a typo somewhere, you might have forgotten to include the script, or forgotten to even write the script, no idea. But you can't GetComponent<PlayerMovement>() if there is no PlayerMovement script.
     
    Yoreki likes this.
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Please have a look at this: http://plbm.com/?p=220

    • The first error tells you that 'enemy' does not exist. What the computer tries to tell you is that you are using something you never defined. In this case 'enemy' at line 18. You probably meant 'other' instead?
    • The second and third error tell you that the compiler cant find "PlayerMovement". Does such a script exist both in name of the file, as well as name of the class? Unity should find it if it exists. So check that first.
     
    Kurt-Dekker and Joe-Censored like this.
  4. camlightbody

    camlightbody

    Joined:
    Nov 21, 2020
    Posts:
    10
    Thank You!