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

Bug string interpolation bug inside macro and namespace

Discussion in 'Scripting' started by wolilio, Jul 15, 2021.

  1. wolilio

    wolilio

    Joined:
    Aug 19, 2019
    Posts:
    28
    when i use @$ for complex string , i get a error message in inspector : "No MonoBehaviour scripts in the file, or their names do not match the file name" ,then i simplify the code as below:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace MySpace
    4. {
    5.     public class TestStr : MonoBehaviour
    6.     {
    7.         void Awake()
    8.         {
    9. #if UNITY_EDITOR
    10.             var str = @$"";
    11. #endif
    12.         }
    13.     }
    14. }
    interesting thing is when delete namespace or "#if xxx" macro ,the error gone
    unity ver:2020.3.1f1

    string bug1.png
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Are you sure you're not getting compiler errors in your console? I do:
    Assets\TestStr.cs(10,27): error CS1525: Invalid expression term ''
    Assets\TestStr.cs(10,27): error CS1646: Keyword, identifier, or string expected after verbatim specifier: @
    Assets\TestStr.cs(10,28): error CS1002: ; expected


    There's warning in the docs: Starting with C# 8.0, you can use the $ and @ tokens in any order: both $@"..." and @$"..." are valid interpolated verbatim strings. In earlier C# versions, the $ token must appear before the @ token.

    If I use $@ instead of @$, everything works fine.
     
    Bunny83 likes this.
  3. wolilio

    wolilio

    Joined:
    Aug 19, 2019
    Posts:
    28
    no error here, i can enter play mode ,of course this script will not load

    it's good.

    what i mean is the unity do support @$ ,but if "@$" is inside namespace and macro ,@$ will cause strange error and this error is not in console,