beginner problem Help Needed Error CS0246

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

2 Likes

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.

2 Likes
Target target = hit.transform.GetComponent<Target>();

Do you have Target.cs anywhere in your project?

2 Likes

I’ve checked the capitalization, and it’s not the issue

1 Like

Would you mind sharing your Target script? It would help with figuring out your problem.

2 Likes

could you pleas explain what a .cs is I’m new to unity and don’t really know what it is

Here’s my Target script for you

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);
        }
    }

}
1 Like

.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.)

2 Likes

I’ve

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.

1 Like

THANKS!!! This article helped a lot !!

I might be ignorant, but how and where would one go about Define-ing something like “Target.cs”?

Thanks much! helped good

Welcome! You do that by creating a script!

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:

Imphenzia / imphenzia - super-basic Unity tutorial:

https://www.youtube.com/watch?v=pwZpJzpE2lQ

Brackeys super-basic Unity Tutorial series:

https://www.youtube.com/watch?v=IlKaB1etrik

Sebastian Lague Intro to Game Development with Unity and C#:

https://www.youtube.com/watch?v=_cCGBMmMOFw

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?

using UnityEngine;

public class Enemy : MonoBehaviour
{
    [SerializedField] private GameObject _cloudParticlePrefab;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        Bird bird = collision.collider.GetComponent<Bird>();
        if (bird != null)
        {
            Instantiate(_cloudParticlePrefab, transform.position, Quaternion.identity);
            Destroy(gameObject);
            return;
        }

        Enemy enemy = collision.collider.GetComponent<Enemy>();
        if (enemy != null)
        {
            return;
        }

        if( collision.contacts[0].normal.y < -0.5)
        {
            Instantiate(_cloudParticlePrefab, transform.position, Quaternion.identity);
            Destroy(gameObject);
        }
    }
}

Please recognize that this statement is not a useful description of what is happening.

Also, please make your own fresh NEW POST. It’s how the forums work.

When you post, keep this in mind:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

If you have a compiler error:

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.

What is so hard to understand in that I wonder:eyes:

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:

7720885--968914--Screen Shot 2021-12-08 at 5.50.42 PM.png

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.