Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I keep getting exception Feature `interpolated strings' cannot be used because it is not part of the C# 4.0 language?

Tags:

c#

unity3d

First I had the exception the error in the visual studio. So I clicked on the line with the error and selected to fix it to change the visual studio target to csharp 6

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class PrefabReplace : EditorWindow
{
    [SerializeField] private GameObject prefab;

    [MenuItem("Tools/Prefab Replace")]
    static void CreateReplaceWithPrefab()
    {
        EditorWindow.GetWindow<PrefabReplace>();
    }

    private void OnGUI()
    {
        prefab = (GameObject)EditorGUILayout.ObjectField("Prefab", prefab, typeof(GameObject), false);

        if (GUILayout.Button("Replace"))
        {
            var selection = Selection.gameObjects.ToList();

            if (prefab != null && selection.Count > 0)
            {
                for (var i = selection.Count - 1; i >= 0; --i)
                {
                    var selected = selection[i];
                    var prefabType = PrefabUtility.GetPrefabType(prefab);
                    GameObject newObject;

                    if (prefabType == PrefabType.Prefab)
                    {
                        newObject = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
                    }
                    else
                    {
                        newObject = Instantiate(prefab);
                        newObject.name = prefab.name;
                    }

                    if (newObject == null)
                    {
                        Debug.LogError("Error instantiating prefab");
                        break;
                    }

                    Undo.RegisterCreatedObjectUndo(newObject, "Replace With Prefabs");
                    newObject.transform.parent = selected.transform.parent;
                    newObject.transform.localPosition = selected.transform.localPosition;
                    newObject.transform.localRotation = selected.transform.localRotation;
                    newObject.transform.localScale = selected.transform.localScale;
                    newObject.transform.SetSiblingIndex(selected.transform.GetSiblingIndex());
                    Undo.DestroyObjectImmediate(selected);
                }
            }
        }

        if (GUILayout.Button("Copy settings"))
        {
            var selection = Selection.gameObjects.ToList();

            for (int i = selection.Count - 1; i >= 0; --i)
            {
                var selected = selection[i].transform;
                string toWrite = $"{selected.parent}:{selected.localPosition}:{selected.localRotation}:{selected.localScale}";
                WriteDataToFile(toWrite);
            }
        }

        GUI.enabled = false;
        EditorGUILayout.LabelField("Selection count: " + Selection.objects.Length);
    }

    private void OnInspectorUpdate()
    {
        Repaint();
    }

    private void WriteDataToFile(string line)
    {
        string path = "Assets/Resources/test.txt";
        string[] text = new string[4];
        if (File.Exists(path))
        {
            text = File.ReadAllLines(path);
            if (!text.Contains(line))
                File.AppendAllText(path, line);
        }
    }
}

The error was on this line in the visual studio:

string toWrite = $"{selected.parent}:{selected.localPosition}:{selected.localRotation}:{selected.localScale}";
                WriteDataToFile(toWrite);

Now the error is gone from the visual studio and I can build it without any problems but in the unity editor the exception still exist in the console and clicking on the Clear button not removing it:

Exception in editor

like image 204
BenBen Shmil Avatar asked Aug 15 '18 00:08

BenBen Shmil


1 Answers

You have to enable .NET 4.x in the Editor in order to use the interpolated strings. feature.

Go to Edit --> Project Settings --> Player --> Other Settings --> Configuration --> Scripting Runtime Version --> .NET 4.x Equivalent.

like image 57
Programmer Avatar answered Oct 18 '22 13:10

Programmer