I have been using a tutorial by Brackeys to help me with shooting with raycast and he is having no issue with anything but I have checked and checked and made mine pretty much identical to his and I can’t fix the problem, the errors
Assets\gunScript.cs(25,56): error CS0246: The type or namespace name ‘Target’ could not be found (are you missing a using directive or an assembly reference?)
Assets\gunScript.cs(25,13): error CS0246: The type or namespace name ‘Target’ could not be found (are you missing a using directive or an assembly reference?)
here’s the code I have written is:
using UnityEngine;
public class gunScript : MonoBehaviour{
public float damage = 10f;
public float range = 100f;
public Camera fpsCam;
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
}
}
}
I’m struggling a lot and it would mean a lot to me if anyone decided to help
Did you make a “Target” script as part of the tutorial? I notice that you didn’t capitalize “gunScript” (conventionally it would be called “GunScript”). Is your “Target” class actually called “target” with a lowercase t? If so, you need to use the same capitalization everywhere. C# is case sensitive. you need to use consistent capitalization.
using UnityEngine;
public class enemy : MonoBehaviour{
public float health = 50f;
public void TakeDamage (float ammount)
{
health -= ammount;
if(health <= 0f)
{
Die();
}
void Die()
{
Destroy(gameObject);
}
}
}
.cs is the file extension used for C# source code files (cs = c sharp)
That class isn’t named Target. It’s named enemy.
Plain C# doesn’t care about file names, only class names.
Unity cares about both, and will give you problems if they’re not the same. (e.g. a MonoBehaviour class named “Target” must be defined in a file called “Target.cs”, a MonoBehaviour class named “Enemy” must be defined in a file called “Enemy.cs”, etc.)
Thank you so much, this solved the problem, I changed the name of the script and didn’t realize that I had to update anything else, I’ll remember this. Thanks everyone who helped.
If you want to really get a HUGE jump on all the “larnin’s” needed to really thrive here, and join us kool kids in writin’ scripts and makin’ magic, check out these awesome basic tutorials:
For some reason when adding a [SerializedField] to my code, it breaks Unity and then give me the same error as above. Here is my code so far, can anyone shed some insight as to why this one line is not being recognized?
The complete error message contains everything you need to know to fix the error yourself.
The important parts of the 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)
also possibly useful is the stack trace (all the lines of text in the lower console window)
Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.
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.
Remember: NOBODY here memorizes error codes. That’s not a thing. 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.
I am new to coding and have no idea of the language or how to describe what is going on. Could be helpful to add an example of what you are talking about and not just tell the poster that what they said doesn’t make sense. This post explained nothing to me that I didn’t already try/do and honestly could have gone without a response.
It is purely telling me how to find the error, but no reference to how to correct it. My code looks ok to me but I am still receiving the “All compiler errors have to be fixed before you can enter playmode.” The other thing I am referring to is “Please recognize that this statement is not a useful description of what is happening.” How am I supposed to explain it so it makes sense since my original post obviously was understood but had to be corrected because it isn’t 100% accurate. It’s not that I don’t get what was said, but nothing was explained other than posting in my own thread.
You clearly did not follow the link I provided. In that link it tells you standard troubleshooting tips that apply to EVERY situation, not just coding. If you are link-challenged, let me screenshot it here for you:
And what did google tell you about fixing those errors?
This will be the last thing I post to this hijacked thread.
Please start your own thread, and keep the four things above in mind.