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 Visual Studio Code does not recognize C# syntax on Unity.

Discussion in 'Scripting' started by CVSeal, Jun 9, 2022.

  1. CVSeal

    CVSeal

    Joined:
    Jun 9, 2022
    Posts:
    6
    When connecting lib. "using.System;" , for some reason, C# syntax is not recognized. parametr convert.ToInt32 doesn't stand out. I'm using Visual Studio Code (with .NET SDK and plugin for C#).
    In Unity itself, VS Code is selected as the code editor (I installed it manually).
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
  3. CVSeal

    CVSeal

    Joined:
    Jun 9, 2022
    Posts:
    6
    For some reason this part is not recognized. Although the library is connected and extensions are installed + system reboot + .NET SDK

    I followed all the steps in this guide, but the situation remains as follows:
    https://ibb.co/XC3WkZj
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,963
    This may help you with intellisense and possibly other Visual Studio integration problems:

    Sometimes the fix is as simple as doing Assets -> Open C# Project from Unity. Other times it requires more.

    Other times it requires you also nuke the userprefs and .vsconfig and other crufty low-value high-hassle files that Visual Studio tends to slowly damage over time, then try the above trick.

    Barring all that, move on to other ideas:

    https://forum.unity.com/threads/intellisense-not-working-with-visual-studio-fix.836599/

    Also, try update the VSCode package inside of Unity: Window -> Package Manager -> Search for Visual Studio Code Editor -> Press the Update button

    Also, this: https://forum.unity.com/threads/no-suggestions-in-vscode.955197/#post-6227874
     
  5. CVSeal

    CVSeal

    Joined:
    Jun 9, 2022
    Posts:
    6
    So I did the appropriate steps, installed all the necessary components, manually specified the code editor in the Unity tools.


    Here's what I came up with.
    https://ibb.co/dgCfg3Z (Tools installed, connected editor)

    But "Convert." Still not recognized.
    https://ibb.co/XC3WkZj

    Honestly, I don't know what's wrong. I watched various videos on YouTube, everything works for people.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,963
    Your pictures aren't working, I just get a blank screen. Use this forum's picture uploading. It works 100%.
     
  7. CVSeal

    CVSeal

    Joined:
    Jun 9, 2022
    Posts:
    6
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,963
    Do you have your own class called Convert?

    When in doubt, always use the fully-qualified name:

    Code (csharp):
    1. x = System.Convert.ToInt32( pos.x);
    Also, if that's all you're doing, just use Mathf.Round or Mathf.Floor or just cast it to an integer:

    Code (csharp):
    1. x = (int)pos.x;
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You're definitely missing something in the VSCode configuration if line 14 of your script is not showing as an error. "IEnumenator"? Same for line 11, you are not invoking the coroutine method properly - it needs () after the method name.
     
  10. CVSeal

    CVSeal

    Joined:
    Jun 9, 2022
    Posts:
    6
    "Do you have your own class called Convert?"

    I doubt it, just followed the steps in the video tutorial for writing the code.

    I am an absolute beginner in programming.I am currently learning (Sorry for stupid questions.)

    It’s just that I don’t quite understand why, under the same conditions, other people recognize without problems, while my system does not respond.
     
  11. CVSeal

    CVSeal

    Joined:
    Jun 9, 2022
    Posts:
    6
    The full code should have looked like this, but I got stuck at the moment of connecting using.System. (Edit in console 0 errors)

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. public class GameController : MonoBehaviour {
    8.  
    9.     private CubePos nowCube = new CubePos(0, 1, 0);
    10.     public float cubeChangePlaceSpeed = 0.5f;
    11.     public Transform cubeToPlace;
    12.     public GameObject cubeToCreate, allCubes;
    13.     private Rigidbody allCubesRb;
    14.  
    15.     private List<Vector3> allCubesPositions = new List<Vector3> {
    16.         new Vector3(0, 0, 0),
    17.         new Vector3(1, 0, 0),
    18.         new Vector3(-1, 0, 0),
    19.         new Vector3(0, 1, 0),
    20.         new Vector3(0, 0, 1),
    21.         new Vector3(0, 0, -1),
    22.         new Vector3(1, 0, 1),
    23.         new Vector3(-1, 0, -1),
    24.         new Vector3(-1, 0, 1),
    25.         new Vector3(1, 0, -1),
    26.     };
    27.  
    28.     private void Start() {
    29.         allCubesRb = allCubes.GetComponent<Rigidbody>();
    30.         StartCoroutine(ShowCubePlace());
    31.     }
    32.  
    33.     private void Update() {
    34.         if (Input.GetMouseButtonDown(0) || Input.touchCount > 0) {
    35.             #if !UNITY_EDITOR
    36.             if (Input.GETTOUCH(0).phase != TouchPhase.Began) return;
    37.             #endif
    38.             GameObject newCube = Instantiate(cubeToCreate, cubeToPlace.position, Quaternion.identity) as GameObject;
    39.             newCube.transform.SetParent(allCubes.transform);
    40.             nowCube.setVector(cubeToPlace.position);
    41.             allCubesPositions.Add(nowCube.getVector());
    42.             allCubesRb.isKinematic = true;
    43.             allCubesRb.isKinematic = false;
    44.             SpawnPosition();
    45.         }
    46.     }
    47.  
    48.     IEnumerator ShowCubePlace() {
    49.         while(true) {
    50.             SpawnPosition();
    51.             yield return new WaitForSeconds(cubeChangePlaceSpeed);
    52.         }
    53.     }
    54.  
    55.     private void SpawnPosition() {
    56.         List<Vector3> positions = new List<Vector3>();
    57.         if (IsPositionEmpty(new Vector3(nowCube.x + 1, nowCube.y, nowCube.z)) && nowCube.x + 1 != cubeToPlace.position.x)
    58.             positions.Add(new Vector3(nowCube.x + 1, nowCube.y, nowCube.z));
    59.         if (IsPositionEmpty(new Vector3(nowCube.x - 1, nowCube.y, nowCube.z)) && nowCube.x - 1 != cubeToPlace.position.x)
    60.             positions.Add(new Vector3(nowCube.x - 1, nowCube.y, nowCube.z));
    61.         if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y + 1, nowCube.z)) && nowCube.y + 1 != cubeToPlace.position.y)
    62.             positions.Add(new Vector3(nowCube.x, nowCube.y + 1, nowCube.z));
    63.         if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y - 1, nowCube.z)) && nowCube.y - 1 != cubeToPlace.position.y)
    64.             positions.Add(new Vector3(nowCube.x, nowCube.y - 1, nowCube.z));
    65.         if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y, nowCube.z + 1)) && nowCube.z + 1 != cubeToPlace.position.z)
    66.             positions.Add(new Vector3(nowCube.x, nowCube.y, nowCube.z + 1));
    67.         if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y, nowCube.z - 1)) && nowCube.z - 1 != cubeToPlace.position.z)
    68.             positions.Add(new Vector3(nowCube.x, nowCube.y, nowCube.z - 1));
    69.         cubeToPlace.position = positions[UnityEngine.Random.Range(0, positions.Count)];
    70.     }
    71.  
    72.     private bool IsPositionEmpty(Vector3 targetPos) {
    73.         if (targetPos.y == 0)
    74.             return false;
    75.         foreach(Vector3 pos in allCubesPositions)
    76.             if (pos.x == targetPos.x && pos.y == targetPos.y && pos.z == targetPos.z)
    77.                 return false;
    78.         return true;
    79.     }
    80. }
    81.  
    82. struct CubePos {
    83.     public int x, y, z;
    84.  
    85.     public CubePos(int x, int y, int z) {
    86.         this.x = x;
    87.         this.y = y;
    88.         this.z = z;
    89.     }
    90.  
    91.     public Vector3 getVector() {
    92.         return new Vector3(x, y, z);
    93.     }
    94.  
    95.     public void setVector(Vector3 pos) {
    96.         x = Convert.ToInt32(pos.x);
    97.         y = Convert.ToInt32(pos.y);
    98.         z = Convert.ToInt32(pos.z);
    99.     }
    100. }
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,963
    Okay, there's the problem. You're just making typos. DO NOT MAKE TYPOS.

    For instance, we know the tutorial did not tell you to put a closing brace on line 17 above. That's just not a thing.

    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, resist the urge to think it isn't your typing. It's probably your typing.

    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.

    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.
     
  13. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,303
    I suggest you use Visual Studio Community, it's much more straight forward, and it's geared towards C# and .Net development in a way that VScode isn't.
     
    PraetorBlue likes this.