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

why I get : the type or namespace name 'SpawnManager' could not be found(are you missing a using dir

Discussion in 'Scripting' started by Shahar_bar2, Aug 4, 2020.

  1. Shahar_bar2

    Shahar_bar2

    Joined:
    Jun 22, 2020
    Posts:
    6
  2. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Open the SpawnManager.cs file to check what namespace is SpawnManager declared inside:
    Code (CSharp):
    1. namespace SomeNamespace
    2. {
    3.     public class SpawnManager
    4.     {
    5.         ...
    6.     }
    7. }
    Then add this namespace at the top of Player.cs:
    Code (CSharp):
    1. using SomeNamespace;
     
    PraetorBlue likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    May or not be related, but something is up with your Unity/Visual Studio integration. The fact that it says "Miscellaneous C# files" in the top left means VS doesn't know what assembly your code belongs to. By default this should say something like "Assembly-CSharp".
     
  4. ScGameDev

    ScGameDev

    Joined:
    Aug 4, 2023
    Posts:
    5
    Hi Guys i have a simmilar problem i belive i does same course that as person who created that post.
    I will post error i get and screens from code below.
    "
    Assets\Scenes\Scripts\Player.cs(20,13): error CS0246: The type or namespace name 'SpawnManager' could not be found (are you missing a using directive or an assembly reference?)
    "





    SPAWN MANGER 1.jpg


    Spawn manager 2.jpg

    Spawn Manager 3.jpg
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You have the same exact problem I mentioned above:
    https://forum.unity.com/threads/why...-you-missing-a-using-dir.945197/#post-6167909

    You need to configure Visual Studio, as per these instructions:
    https://learn.microsoft.com/en-us/v...ndows#install-unity-support-for-visual-studio

    As for your actual code problem - Spawn_Manager is not the same as SpawnManager.
     
    bugfinders likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Yes, I agree: you are also just making silly typing mistakes.

    Stop doing that because the computer is going to FORCE you to fix them all before you can do anything.

    So why not type it correctly to start with?

    When you make a typing mistake, you do not need the forum. You need to fix your typing. Here's how:

    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.

    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.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    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.


    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:
     
    bugfinders likes this.
  7. ScGameDev

    ScGameDev

    Joined:
    Aug 4, 2023
    Posts:
    5


    Hi thank you very much for quick answer i really appreciate it.
    I did fixed VS studio-Unity link and it does saids "Assembly-CSharp". Thank you very much for that.

    I cant get around second issue. I tried to change the name but it pop out another Error. I Quote error below.

    "Assets\Scenes\Scripts\Player.cs(4,7): error CS0138: A 'using namespace' directive can only be applied to namespaces; 'Spawn_Manager' is a type not a namespace. Consider a 'using static' directive instead"


    Would be very greatfull if you could point out which line of code i should to replace the "SpawnManager"
    I will try to explain what the code should to do and what i acctualy did in unity.

    Inside Spawn_Manager script there is a variable "_stopSpawning" that has a booliant value set to it.
    It used in While Loop that was created in intention of constant enemy spawning till player is dead which is variable created on line 50 of "public void OnPlayerDeath()"


    Inside Player script (We want our player to communicate with Spawn_manager so if player dies enemies stop spawning - find the object and get component)
    I got variable "private Spawn_Manager _spawnManager;" on line 21 - But i dont get it why is it this way (I fallow unity course and structure everything just like in the acctual course.

    I got variable that look in to game object named "Spawn_Manager" and trying to get component. in course it saids <component> should always match with orignal code from line 21 - "SpawnManager"
    _spawnManager = GameObject.Find("Spawn_Manager").GetComponent<SpawnManager>();


    Im sorry that my explenation is not clear but i just began doing this unity course and these are my first steps with CSHARP and Unity and tbh there are points where i just get lost.



    IN UNITY :
    I got "Player" game object which is linked to "Player" script.
    I got as well "Spawn_Manager" game object that its linked to "Spawn_Manager" script.
    "Spawn Manager" game object contains "Enemy_Container" game object.

    IN Virtual Studio :

    Player SCRIPT :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Spawn_Manager;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     //if varieble is public other game objects can manipulate it like power ups. if its private other objects or scripts cant manipulate is value.
    9.     //[Serial Field] alows us to manipulate value from unity inspector even if its private.
    10.     [SerializeField]
    11.     private float _speed = 6.3f;
    12.     [SerializeField]
    13.     private GameObject _laserPrefab;
    14.     [SerializeField]
    15.     private float _fireRate = 0.2f;
    16.     [SerializeField]
    17.     private float _canFire = -1f;
    18.     [SerializeField]
    19.     private int _lifes = 3;
    20.  
    21.     private Spawn_Manager _spawnManager;
    22.  
    23.  
    24.  
    25.  
    26.     public float horizontalInput;
    27.     public float verticalInput;
    28.  
    29.     // Start is called before the first frame update
    30.     void Start()
    31.     {
    32.         // take the curent position = new position (0,0,0)
    33.        
    34.        
    35.         transform.position = new Vector3(0, 0, -3);
    36.  
    37.         _spawnManager = GameObject.Find("Spawn_Manager").GetComponent<SpawnManager>();
    38.  
    39.         if (_spawnManager == null)
    40.         {
    41.             Debug.LogError("The Spawn Manager is NULL.") ;
    42.  
    43.         }
    44.  
    45.     }
    46.  
    47.     // Update is called once per frame
    48.     void Update()
    49.      
    50.     {
    51.         CalculateMovement();
    52.  
    53.         if (Input.GetKeyDown(KeyCode.Space) && Time.time > _canFire)
    54.         {
    55.             FireLaser();
    56.         }
    57.  
    58.     }
    59.     void CalculateMovement()
    60.     {
    61.         float horizontalInput = Input.GetAxis("Horizontal");
    62.         float verticalInput = Input.GetAxis("Vertical");
    63.  
    64.         float tiltSideAngle = -35.5f;
    65.         float tiltFrontAngle = 15.9f;
    66.         float smooth = 7.9f;
    67.  
    68.         //Expelanation of what happen below Vector 3 (1,0,0) * 0  *  * real time
    69.         //transform.Translate(Vector3.right * horizontalInput * _speed * Time.deltaTime);
    70.         //transform.Translate(Vector3.up * verticalInput * _speed * Time.deltaTime);
    71.  
    72.         // More optimised version of code above.
    73.         // transform.Translate(new Vector3(horizontalInput, verticalInput, 0) * _speed * Time.deltaTime);
    74.  
    75.  
    76.         // Even more optimised version of code above.
    77.  
    78.         Vector3 direction = new Vector3(horizontalInput, 0, verticalInput);
    79.  
    80.         transform.Translate(direction * _speed * Time.deltaTime);
    81.      
    82.  
    83.         float tiltAroundZ = Input.GetAxis("Horizontal") * tiltSideAngle;
    84.         float tiltAroundX = Input.GetAxis("Vertical") * tiltFrontAngle;
    85.  
    86.         Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);
    87.         transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
    88.  
    89.  
    90.         // if plaer position on the y is greater then 0
    91.         // y position = 0
    92.         // else if position on the y is > then -0.89f
    93.         // y pos =-0.89f
    94.  
    95.         if (transform.position.z >= 14.4f)
    96.         {
    97.             transform.position = new Vector3(transform.position.x, 0, 14.4f);
    98.         }
    99.         else if (transform.position.z <= -10.6f)
    100.         {
    101.             transform.position = new Vector3(transform.position.x, 0, -10.6f);
    102.         }
    103.  
    104.         if (transform.position.x >= 22.85f)
    105.         {
    106.             transform.position = new Vector3(22.85f, 0, transform.position.z);
    107.         }
    108.         else if (transform.position.x <= -22.85f)
    109.         {
    110.             transform.position = new Vector3(-22.85f, 0, transform.position.z);
    111.         }
    112.         if (transform.position.y >= 0.0f)
    113.         {
    114.             transform.position = new Vector3(transform.position.x , 0 , transform.position.z);
    115.         }
    116.         else if (transform.position.y <=0)
    117.             transform.position = new Vector3(transform.position.x, 0, transform.position.z);
    118.         if (tiltAroundX <= -3)
    119.         {
    120.             tiltAroundX = -3;
    121.         }
    122.         // if player on the x is > 11
    123.         // x position = - 11
    124.         //else if player on the x < -11
    125.         // x position = -11
    126.     }
    127.     void FireLaser()
    128.     {
    129.         //if i press space key
    130.         // spawn game object  
    131.         {
    132.             _canFire = Time.time + _fireRate;
    133.             Instantiate(_laserPrefab, transform.position + new Vector3(0,0,1), Quaternion.identity);
    134.         }
    135.     }
    136.  
    137.  
    138.     public void Damage()
    139.     {
    140.         //_lifes = _lifes -1
    141.         //_lifes -= 1;
    142.         //_lifes --
    143.         _lifes--;
    144.         //check if daad
    145.         //destroy us
    146.         if (_lifes < 1)
    147.         {
    148.             _spawnManager.OnPlayerDeath();
    149.  
    150.             // Comunicate with spawn manger
    151.             // let it know to stop spawning enemies.
    152.  
    153.  
    154.             Destroy(this.gameObject);
    155.  
    156.  
    157.  
    158.         }
    159.  
    160.     }
    161.  
    162.  
    163. }

    SPAWN_MANAGER SCRIPT :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spawn_Manager : MonoBehaviour
    6. {
    7.  
    8.  
    9.     [SerializeField]
    10.     private GameObject _enemyPrefab;
    11.     [SerializeField]
    12.     private GameObject _Enemy_Container;
    13.  
    14.     private bool _stopSpawning = false;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         StartCoroutine(SpawnRoutine());
    20.        
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.        
    27.  
    28.      
    29.     }
    30.  
    31.     // spawn game object every 5 second
    32.     //create courtine of type IEmulator -- Yield Events
    33.  
    34.     IEnumerator SpawnRoutine()
    35.     {
    36.         while (_stopSpawning == false)
    37.         {
    38.  
    39.             Vector3 posToSpawn = new Vector3(Random.Range(-23.5f, 23.5f), 0, 19.5f);
    40.             GameObject newEnemy = Instantiate(_enemyPrefab, posToSpawn, Quaternion.identity);
    41.             newEnemy.transform.parent = _Enemy_Container.transform;
    42.             yield return new WaitForSeconds(7.0f); // wait 7  seconds
    43.        
    44.         }
    45.  
    46.         //while loop (infinite loop)
    47.         //instantiate enemy prefab
    48.         // yield wait for 5 seconds
    49.     }
    50.     public void OnPlayerDeath()
    51.     {
    52.         _stopSpawning = true;
    53.  
    54.  
    55.     }
    56. }
    57.  


    Regards.

     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    There is no need for
    using Spawn_Manager;
    and all it's doing is causing an error. Simply delete that line.

    As for
    GetComponent<SpawnManager>();
    again the name has to match everywhere. Since your class is called Spawn_Manager, you would have to write
    GetComponent<Spawn_Manager>();


    However I would recommend always following C# naming conventions and renaming it to
    SpawnManager
    everywhere instead.

    https://learn.microsoft.com/en-us/d...ing-style/identifier-names#naming-conventions
     
    ScGameDev likes this.
  9. ScGameDev

    ScGameDev

    Joined:
    Aug 4, 2023
    Posts:
    5

    Hi thank you very much for quick answer and lots of useful advices its looks like you have a lot of experience.
    This is my first unity course, first unity project ever, and first c sharp script ever.
    I came across this problem and what i DID - IS ACCTUALY EXACT THING THAT YOU ADVICED ME TO DO.
    I WENT GOOGLE AND GOOGLE IT.
    AND IT ACCTUALY TOOK ME HERE.
    BECAUSE JUST LIKE YOU SAID - SOMEONE ELSE ALREADY HAD THIS PROBLEM.
    THIS IS WHY IM POSTING HERE I HAD IMPRESION THAT THIS IS EXACLY WHAT I SHOULD TO DO CHECK IF SOMEONE ELSE HAD THIS PROBLEM AND SEE DID THEY FIXED IT OR NOT IF NOT THEN ASK.
    IM A BIT CONFUSED AS YOU GIVING ME ADVICES AND AT SAME TIME SEEMS TO BE NOT HAPPY ABOUT ME FULLFILING SOME OF THOSE ADVICES.

    I tried to explained it to plant but as this is my VERY FIRST C SHARP PROJECT EVERY i just dont get it yet.

    Thank you for all of the pro advices.
    Be blessed
     
  10. ScGameDev

    ScGameDev

    Joined:
    Aug 4, 2023
    Posts:
    5

    Thank you very much.

    #


    Thank you very much i think i will have to put some time to C sharp it self before i countinue with that course.

    From video i know that component name has to match but i dont undestand that part "Since your class is called Spawn_Manager, you would have to write
    GetComponent<Spawn_Manager>"

    Why component inside "<>" should have same name as class if code should to get.component not class.
    Is component and class the same thing ??? Im sorry but i just got lost.
    I think i will do some c sharp basic videos and then back to that course as its my first c sharp experience and there wasnt really much explained about it so i just my go study it first.

    Regards.
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    PLEASE stop necro-posting to someone else's 2020 thread.

    Go start your own thread. It's FREE!

    How to report your problem productively in the Unity3D forums:

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

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?


    Unity's component architecture

    https://forum.unity.com/threads/custom-gameobject.967582/#post-6299125

    Terminology primer for assets:

    https://forum.unity.com/threads/con...nto-materials-for-quads.1369056/#post-8632563

    Try this approach: ask yourself again and again... "Can I?"

    Imphenzia: How Did I Learn To Make Games:

     
    ScGameDev likes this.
  12. ScGameDev

    ScGameDev

    Joined:
    Aug 4, 2023
    Posts:
    5
    Hi


    Hi thank you very much.
    Be blessed for the links and all of the advices !!
    I will check the links now, maybe expect that first link you post as for some reason its not HTTPS but HTTP and that mean that its not SECURED and its its just to weird that any legit website in 2023 wouldnt be HTTPS as its the most basic thing that you could do to secure connection. So i may just stay away from this one as i doesnt look legit, but definitely will check rest of them.

    Once again thank you for the advices be blessed !!!
     
  13. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You wrote:
    Code (CSharp):
    1. public class Spawn_Manager : MonoBehaviour
    Therefore the name of your class is Spawn_Manager. That's all.
     
    ScGameDev likes this.
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    The most blessed advice by far is to stop necro-posting to someone else's thread from 2020.

    The second-most-blessed advise is to start your own thread, it's FREE.

    How to report your problem productively in the Unity3D forums:

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

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?
     
    ScGameDev likes this.