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

C# scripts aren't being found by Visual Studios.

Discussion in 'Scripting' started by JetStreamSham, Dec 14, 2017.

  1. JetStreamSham

    JetStreamSham

    Joined:
    Jul 4, 2015
    Posts:
    27
    Whenever I create a new c# script in Unity it doesn't register in Visual Studios. I created an empty script called Player and made a variable in PlayerStateMachine script.

    Code (CSharp):
    1. public class PlayerStateMachine {
    2.  
    3.     Player s;
    4.  
    5.     Dictionary<string, IPlayerState> states = new Dictionary<string, IPlayerState>();
    6.  
    7. }
    8.  
    It gives me an error saying: "The type or namespace name 'Player' could not be found ( are you missing a directive or an assembly reference?)".

    Here's the directory layout for my Unity project. Everything is inside the asset folder.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Is this a new project or one that you updated to a new version of Unity?
     
  3. JetStreamSham

    JetStreamSham

    Joined:
    Jul 4, 2015
    Posts:
    27
    I can't recall, is there some way to check?
     
  4. Comafly

    Comafly

    Joined:
    May 30, 2014
    Posts:
    87
    Close both VS and Unity.

    Reopen just Unity, and then double-click on any one of your script files from inside of Unitys file browser.

    I get this issue sometimes as well and the above fixes it. Hope it works for you, too!
     
  5. JetStreamSham

    JetStreamSham

    Joined:
    Jul 4, 2015
    Posts:
    27
    I tried your solution and it did not work for me.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Try removing the csproj and sln files from your project folder, then restart Unity and double click a script. The files will be recreated for you.
     
  7. JetStreamSham

    JetStreamSham

    Joined:
    Jul 4, 2015
    Posts:
    27
    That worked. Thank you so much. Do you have any idea why this happens and what I can do to fix it permanently?
     
  8. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    Create your scripts directly in Visual Studio.
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    No need to make your scripts directly in VS.

    From what I understand is sometimes when you upgrade a project (say from Unity 5.6 to 2017.1) it can create bugs such as the one you experienced, which is why I was asking if your project had been upgraded or was new. If it happens outside of upgrading to a new version, you may have another issue.

    Glad it's working again for you.
     
  10. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    Well, I consider such a workflow, when you create a script in Unity and then you switch to Visual Studio to edit it, to be kinda weird.