Search code examples
c#unity-game-enginereturnmonodevelopinternal-compiler-error

C# - Unity Internal Compiler Error: "System.Runtime.CompilerServices.CallSite" CS0518 - when using dynamic variable


Using MonoDevelop 4.0.1 to code for Unity 4.5.2f1

The code below is part of the code that I am using, all the variables have been checked using Debug.Log and they all (individually) return the correct values.

When I refresh my script asset in Unity, I get the following error message:

Internal compiler error. See the console log for more information. output was:error CS0518: The predefined type `System.Runtime.CompilerServices.CallSite' is not defined or imported

error CS0518: The predefined type System.Runtime.CompilerServices.CallSite1' is not defined or imported

error CS0518: The predefined type `System.Runtime.CompilerServices.CallSite' is not defined or imported

error CS0518: The predefined type System.Runtime.CompilerServices.CallSite1' is not defined or imported

error CS0518: The predefined type `System.Runtime.CompilerServices.CallSite' is not defined or imported

error CS0518: The predefined type System.Runtime.CompilerServices.CallSite1' is not defined or imported

error CS0518: The predefined type `System.Runtime.CompilerServices.CallSite' is not defined or imported

error CS0518: The predefined type System.Runtime.CompilerServices.CallSite1' is not defined or imported

error CS0518: The predefined type `System.Runtime.CompilerServices.CallSite' is not defined or imported

error CS0518: The predefined type System.Runtime.CompilerServices.CallSite1' is not defined or imported

error CS0518: The predefined type `System.Runtime.CompilerServices.CallSite' is not defined or imported

error CS0518: The predefined type System.Runtime.CompilerServices.CallSite1' is not defined or imported

Here are the sections of my script (simplified) causing this error:

Note: I cannot break my function up into multiple little functions. My function must remain as is I can edit the return statement as needed

using UnityEngine;
using System.Collections;
using Random = UnityEngine.Random;

public class myScene : MonoBehaviour
{
    private dynamic someVar;
    private float myFloat = 1.1f;
    private string myString = "string";
    private int myInt = 2;

    void OnGUI()
    {
        someVar = myFunction(myFloat, myString, myInt);
    }

    //First Function
    public dynamic myFunction(float myFloat, string myString, int myInt)
    {
        //Do something
        dynamic myOtherFunction(myFloat, myString, myInt);
        float myFloat = myOtherFunction.myFloat;
        string myString = myOtherFunction.myString;
        int myInt = myOtherFunction.myInt;
        //Do something
        return new {myFloat = myFloat, myString = myString, myInt = myInt};
    }

    //Second function
    public dynamic myOtherFunction(float myFloat, string myString, int myInt)
    {
        //Do something
        return new {myFloat = myFloat, myString = myString, myInt = myInt};
    }
}

I have not been able to figure out what the issue is, (yes, even with using Google, although some possible solutions came up, I tried them, they did not seem to resolve my issue).

I need to be able to return multiple variables to the OnGUI() function from another function. The method I am using return new{varName = value, ...}; I found here. I cannot use Tuples as Unity 4 MonoDevelop does not support it (according to this post) where a user posted:

No, Unity does not support Tuples. Maybe when Unity upgrades its Mono version to support features that came in .NET Framework 4.

Ultimately, I would like to implement the following code in the OnGUI() function:

myFloat = myFunction.myFloat;
myString = myFunction.myString;
myInt = myFunction.myInt

Any help with this is greatly appreciated, Thank you


Solution

  • You're running into the same problem you did with Tuples. dynamic was introduced in .Net Framework 4.0 and requires the dynamic language runtime (DLR), which isn't present in Unity's version of Mono.

    That being said, given the context you have here your note stating "My function must remain as is I can edit the return statement as needed" shows you don't need your current setup at all. I really can't see why you can't use an object to hold the values (the names you used are making it very hard to come up with logical names for an example):

        public class MyFunctionResult
        {
            public readonly float MyFloat;
            public readonly string MyString;
            public readonly int MyInt;
    
            public MyFunctionResult(float myFloat, string myString, int myInt)
            {
                MyFloat = myFloat;
                MyString = myString;
                MyInt = myInt;
            }
        }
    
        void OnGUI()
        {
            float myFloat = 1;
            string myString = "";
            int myInt = 2;
            var finalResult = myFunction(myFloat, myString, myInt);
            Debug.Log(finalResult.MyFloat); //...
        }
    
        //First Function
        public MyFunctionResult myFunction(float myFloat, string myString, int myInt)
        {
            //Do something
            var myOtherFunctionResult = myOtherFunction(myFloat, myString, myInt);
            float myFloatResult = myOtherFunctionResult.MyFloat;
            string myStringResult = myOtherFunctionResult.MyString;
            int myIntResult = myOtherFunctionResult.MyInt;
            //Do something
            return new MyFunctionResult(myFloat, myString, myInt);
        }
    
        //Second function
        public MyFunctionResult myOtherFunction(float myFloat, string myString, int myInt)
        {
            //Do something
            return new MyFunctionResult(myFloat, myString, myInt);
        }
    

    I tried really hard to come up with an even marginally valid reason and couldn't (but I'm wondering if your simplification is hiding a hidden problem). That being said, you can also use out parameters, but I'd definitely call it a misuse:

    public dynamic myFunction(float myFloat, string myString, int myInt)
    {
        //Do something
    
        float myFloat;
        string myString;
        int myInt;
        myOtherFunction(out myFloat,out myString,out myInt);
        ...
    }
    public void myOtherFunction(out float myFloat, out string myString, out int myInt)
    {
        myFloat = someVal;
        myString = otherVal;
        myInt = thirdVal;
    
        return;
    }