Actionscript example does not work.

I’m trying to get the ActionScript example from the script reference to work. However, when I run it I get an error:

InvalidOperationException: Operation is not valid due to the current state of the object
UnityEngine.Flash.ActionScript.Expression[Int32] (System.String formatString, System.Object arguments)
GameController.Start () (at Assets/Scripts/GameController.cs:14)

My calling script looks like this:

using UnityEngine;
using UnityEngine.Flash;
using System.Collections;

public class GameController : MonoBehaviour {

 // Use this for initialization
 void Start () {
 
 #if UNITY_FLASH
 print ("Yup, Unity Flash");
 ActionScript.Import("com.mycompany.mytool");
        string name = "Lerpz";
        int protocolversion = ActionScript.Expression<int>("com.mycompany.mytool.GetProtocolVersionFromServer({0})", name);
 print (protocolversion);
    #endif
     }
     
     // Update is called once per frame
     void Update () {
     }
    }

That script is attached to the main camera.

My ActionScript looks like this:

package com.mycompany 
{
public final class mytool
{
public static function GetProtocolVersionFromServer(name: String): int
{
//talk the the flash api here.
return 18;
}
}
}

It is named mytool.as and is placed in a folder call ActionScript at the root of the project hierarchy.

I feel I’ve followed the example to the tee, but it’s not working. What can I do to make it work?

Hi,

You’ll get that error if you try to run the example in editor when your current target platform is set to Flash export. This is because the UNITY_FLASH check will be true in this case. If you do not want the code to run in editor you can do one of the following:

  • Add an extra check to ensure this code isn’t run in editor: #if UNITY_FLASH && !UNITY_EDITOR
  • Or, only run the code if the application is running in the flash player: if (Application.platform == RuntimePlatform.FlashPlayer)

As a side note, if you do want your code to work in editor (or for other platforms), you’ll need to create a C# or JS version of your ActionScript class. This will then be used in place of your ActionScript for all export options other than Flash. There are some examples of how you can do this on the Unity manual:
http://docs.unity3d.com/Documentation/Manual/flashexamples-callingflashfunctions.html
http://docs.unity3d.com/Documentation/Manual/flashexamples-supplyingdata.html