Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

The type or namespace name 'Player' could not be found, how can i fix it?

Discussion in 'Scripting' started by ProgramandoHistorias, Jun 27, 2020.

  1. ProgramandoHistorias

    ProgramandoHistorias

    Joined:
    Apr 1, 2020
    Posts:
    11
    I am making a Top-down 2D shooter for a game jam that i'm participating, but when i did the Enemy script for it to damage the player, i got this error : The type or namespace name 'Player' could not be found (are you missing a using directive or an assembly reference?) Here is the line of code that i'm having problems with:

    Line 9
    Code (CSharp):
    1.     private Player player;
    2.  
    Line 13
    Code (CSharp):
    1.  player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
    And here is the full script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour
    6. {
    7.     public float speed;
    8.     private Transform playerPos;
    9.     private Player player;
    10.  
    11.     void Start()
    12.     {
    13.         player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
    14.         playerPos = GameObject.FindGameObjectWithTag("Player").transform;
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         transform.position = Vector2.MoveTowards(transform.position, playerPos.position, speed * Time.deltaTime);
    21.     }
    22.  
    23.     void OnTriggerEnter2D(Collider2D other)
    24.     {
    25.         if(other.CompareTag("Player"))
    26.         {
    27.             player.health--;
    28.             Destroy(gameObject);
    29.         }
    30.         if(other.CompareTag("Projectile"))
    31.         {
    32.             Destroy(gameObject);
    33.         }
    34.     }
    35. }
    36.  
     
  2. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    What is 'Player'?
    Do you have an Component/script called Player.cs in your scene?
    If it's a component in your scene, try using
    GameObject player;
     
    Last edited: Jun 27, 2020
    Kanji90 likes this.
  3. nmintoh

    nmintoh

    Joined:
    Jun 5, 2021
    Posts:
    1
    Create a New Script and Add paste the code below:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     public float speed;
    9.     private Rigidbody2D rigidbody;
    10.    
    11.     void Start()
    12.     {
    13.         rigidbody = GetComponent<Rigidbody2D>();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         rigidbody.velocity = new Vector2(speed, rigidbody.velocity.y);
    20.     }
    21. }
    22.  
     

    Attached Files: