i was following a tutorial about 2d shooting in unity to see id it would help with my script and some of it did so i copied some of that code into my game in its own separate script, i then saved the script went back to the unity editor and was met with this error Assets/enemys/patroll guard/ patrollLook.cs(6,7): error cs0246: the type or namespace ‘player’ could not be found(are you missing a using directive or an assembly reference?)
here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class patrollLook : MonoBehaviour
{
public float seeDistance, damage;
public Transform tr;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
RaycastHit2D hit = Physics2D.Raycast(tr.position, tr.TransformDirection(Vector2.up), seeDistance);
Debug.DrawRay(tr.position, tr.TransformDirection(Vector2.up), Color.green);
if (hit)
{
Debug.Log(hit.transform.name);
player Player = hit.transform.GetComponent<playerMasterScript>();
if (Player)
{
Player.TakeDamage(damage);
}
}
}
}
I’m guessing the line player Player = hit.transform.GetComponent<playerMasterScript>();
Should be var Player = hit.transform.GetComponent<playerMasterScript>();
Are you sure you copied both the script and the error message right? Because the line number doesn’t align with your code here…
I urge you to read and implement the Microsoft C# naming convention / code style.
It is hard to read your code when you are using uncapitalized class names and capitalized local variables when the quasi standard is the other way around.
Remember: NOBODY memorizes error codes. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
The important parts of an error message are:
the description of the error itself (google this; you are NEVER the first one!)
the file it occurred in (critical!)
the line number and character position (the two numbers in parentheses)
All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.
How to understand compiler and other errors and even fix them yourself: