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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question beginner problem Help Needed Error CS0246

Discussion in 'Scripting' started by FirstBB8Droid, May 30, 2020.

  1. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    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:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class gunScript : MonoBehaviour{
    4.  
    5.     public float damage = 10f;
    6.     public float range = 100f;
    7.  
    8.     public Camera fpsCam;
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.         if (Input.GetButtonDown("Fire1"))
    13.         {
    14.             Shoot();
    15.         }
    16.        
    17.     }
    18.     void Shoot()
    19.     {
    20.         RaycastHit hit;
    21.         if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
    22.         {
    23.             Debug.Log(hit.transform.name);
    24.  
    25.             Target target = hit.transform.GetComponent<Target>();
    26.             if (target != null)
    27.             {
    28.                 target.TakeDamage(damage);
    29.             }
    30.         }
    31.     }
    32. }
    33.  
    I'm struggling a lot and it would mean a lot to me if anyone decided to help
     
    Tarun8127 and Tackit_ like this.
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    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.
     
    FirstBB8Droid and Lurking-Ninja like this.
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    Code (CSharp):
    1. Target target = hit.transform.GetComponent<Target>();
    Do you have Target.cs anywhere in your project?
     
    Tarun8127 and FirstBB8Droid like this.
  4. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    I've checked the capitalization, and it's not the issue
     
    Tarun8127 likes this.
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Would you mind sharing your Target script? It would help with figuring out your problem.
     
    Tarun8127 and FirstBB8Droid like this.
  6. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    could you pleas explain what a .cs is I'm new to unity and don't really know what it is
     
  7. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    Here's my Target script for you
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class enemy : MonoBehaviour{
    5.  
    6.     public float health = 50f;
    7.  
    8.     public void TakeDamage (float ammount)
    9.     {
    10.         health -= ammount;
    11.         if(health <= 0f)
    12.         {
    13.             Die();
    14.         }
    15.         void Die()
    16.         {
    17.             Destroy(gameObject);
    18.         }
    19.     }
    20.  
    21. }
     
    Tackit_ likes this.
  8. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    .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.)
     
    Tackit_ and FirstBB8Droid like this.
  9. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    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.
     
    PraetorBlue likes this.
  10. Np7J

    Np7J

    Joined:
    Apr 1, 2021
    Posts:
    1
    THANKS!!! This article helped a lot !!
     
  11. Palliotisreal

    Palliotisreal

    Joined:
    May 23, 2021
    Posts:
    2
    I might be ignorant, but how and where would one go about Define-ing something like "Target.cs"?
     
  12. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
  13. Palliotisreal

    Palliotisreal

    Joined:
    May 23, 2021
    Posts:
    2
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    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:



    Brackeys super-basic Unity Tutorial series:



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

     
  15. Swagdos

    Swagdos

    Joined:
    Dec 9, 2021
    Posts:
    5
    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?
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Enemy : MonoBehaviour
    4. {
    5.     [SerializedField] private GameObject _cloudParticlePrefab;
    6.  
    7.     private void OnCollisionEnter2D(Collision2D collision)
    8.     {
    9.         Bird bird = collision.collider.GetComponent<Bird>();
    10.         if (bird != null)
    11.         {
    12.             Instantiate(_cloudParticlePrefab, transform.position, Quaternion.identity);
    13.             Destroy(gameObject);
    14.             return;
    15.         }
    16.  
    17.         Enemy enemy = collision.collider.GetComponent<Enemy>();
    18.         if (enemy != null)
    19.         {
    20.             return;
    21.         }
    22.  
    23.         if( collision.contacts[0].normal.y < -0.5)
    24.         {
    25.             Instantiate(_cloudParticlePrefab, transform.position, Quaternion.identity);
    26.             Destroy(gameObject);
    27.         }
    28.     }
    29. }
     
  16. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    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.
     
  17. Swagdos

    Swagdos

    Joined:
    Dec 9, 2021
    Posts:
    5
    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.

     
  18. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    What is so hard to understand in that I wondero_O
     
  19. Swagdos

    Swagdos

    Joined:
    Dec 9, 2021
    Posts:
    5
    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.
     
  20. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    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:

    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.