Five Common AS3 to UnityScript Translations
i’ve been using Unity3D on and off for a week now. i’d burned through about a dozen beginner tutorials, and am a little overwhelmed by how much i don’t know about the engine, which enables you to create 3D games to deploy to a number of platforms, including the iPhone and web browsers.
i’d look like a fool if i tried to write a tutorial at this point. So instead, i’ll share this list of translations of common Actionscript 3 coding tasks to the Unity3D engine’s JavaScript-like UnityScript language.

1. Render an Object Visible or Invisible
Actionscript 3:
thing.visible = true; // or false
UnityScript:
thing.renderer.enabled = true; // or false
2. Tint an Object
Actionscript 3:
var colorTransform:ColorTransform = new ColorTransform(); colorTransform.color = 0xFF0000; thing.transform.colorTransform=colorTransform;
UnityScript:
thing.renderer.material.color = Color.red;
3. Get a Random Number
Actionscript 3:
var someRandomNumberBetween0And5:int = Math.floor(Math.random()*5);
UnityScript:
var someRandomNumberBetween0And5:int = Random.Range(0, 5);
4. Respond to a Key Press
Actionscript 3:
stage.addEventListener(KeyboardEvent.KEY_DOWN, doKeyDown); private function doKeyDown(e:KeyboardEvent):void { switch(e.keyCode) { case 32: // Space bar! Do stuff. break; default: break; } }
UnityScript:
if(Input.GetKeyDown("space")) { // Do stuff. }
5. Open a Web Page
Actionscript 3:
navigateToURL(new URL("http://www.untoldentertainment.com"), "_self");
UnityScript:
Application.OpenURL("http://www.untoldentertainment.com");
Popularity: 3% [?]





Email This Post
good stuff Ryan. Unity certainly has peps buzzing – I’m starting to wade into it as well. if you get a chance , keep us posted an any growing pains you encounter as I’m sure others (and myself) will run into similar issues. BTW – I’ve ordered this book which I’m hoping will help out: http://www.packtpub.com/unity-game-development-essentials/book
Thanks, samBrown. The only snags i’ve hit have been a benign error message at the bottom of the screen whenever i exit Play mode, and a very important lesson about the different between “F” and CTRL+F (“F” centers the selected object in the view. i don’t know what CTRL+F does, but it messes up my object’s transform)
Please let us all know if that book is worth the cashola! Next week, i’ll list the tutorials i’ve been following, and i’ll star the ones i think were worth the time.
- Ryan
The funny thing about this type of comparison is that if you were to do it with AS2 instead of AS3 both scripting languages would look very similar! While with AS3 seems that Adobe has greatly complicated all these very simple things, odd… More reason for people who are intimated by AS3 and the new flash to just head on over to Unity I guess.
Joe – that’s one thing that really jumped out at me while i was putting this together. The AS3 examples involve quite a bit more typing. And i’ve even ommitted the necessary import statements, and didn’t include bits that required method declarations, like the keyboard example.
i’m really enjoying the simplicity of UnityScript, and i get a little guilty shiver every time i drag n’ drop a piece of code onto an object … i keep worrying that an egghead will jump out from under my desk and throttle me for bad coding practices.
In many ways, Unity3D makes me feel like i did when i was just starting out with Flash at version 4, back in 2000. There’s all kinds of stuff i want to do … i don’t know how to do most of it … but here i am, willing to keep straining at it until i figure everything out.
- Ryan
That’s great! I love seeing them next to each other like that!
Not bad, it’s always a good thing to record your thoughts when you’re learning something new. Posting them up allows other newcomers to the platform is definitely beneficial to others.
You might want to note that when checking for input you want to do that within some sort of loop, in Unity being a MonoBehaviour’s Update() or FixedUpdate() methods, or (if you want precise control that isn’t checking *all* of the time) inside of a Coroutine.
Cheers!
Thanks, Joseph P. As with the AS3 examples, i ommitted some of the surrounding code structure. You’re right – that input example should be inside one of the looping methods you mentioned. i have heard the term “Coroutine” in the IRC channel, but i don’t know what it means yet.
Agreed Ryan. One thing I have a fear of is that with all these new amazing tools coming out that we will start getting game developers who are so used to “scripting” and don’t really know how to do anything else when it comes to developing software. They will be so used to dragging and dropping code, that the whole idea of OOP, design patterns, all that good stuff will be forgotten. They will also have an excuse to because they have this excellent tool doing all the work for them… We will begin to be flooded with “dumb” game developers. I don’t know perhaps I am a bit to cynical about it all, but just a fear I have. Either way I am getting more and more excited for the day when I can really sit down and start digging into Unity, the comparison makes me even more happy to do it!
Joe B – i think we just have to stay a step ahead of the “dumb” devs *cough* FLASH *cough*. Once everyone jumps ship to Unity and messes the place up a few years from now, we’ll have our sites set on the next big thing.
A Coroutine is a type of loop where you can explicitly break up its functionality into multiple frames using yield commands. It is invaluable when you’re scripting something sequential like a transition, as you won’t need a cubic sh*t ton of booleans inside an Update() loop for early outs if it is something that only happens once. It keeps everything modular and logical.
In fact, I think I just found my first topic for my first InfiniteUnity3D blog post. Thanks for that!
Joseph P. – good idea! But i’m still having trouble picturing it without a ferinstance. Can you give me a ferinstance in this case? (Of course, i can also sit tight and wait for your blog post :)
[...] Read the rest at Untold Entertainment! [...]
I’ll try and have it up this weekend :)
First off, I think it’s great that you’re posting this kind of stuff. I’m hoping to eventually move on to Unity, and I could see resources like this being very useful in making the transition.
Here’s a little more info on coroutines in Unity3D I found through google real quick:
http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html
This is something I really wish AS3 had. There are two big uses I can think of immediately for this:
1. When processing a large amount of data in Flash, the function will hold up everything else and essentially freeze your application. As it is now, it takes a bit of trickery to get any sort of function to work asynchronously. With coroutines, you could just add in a yield as part of a loop to fix it.
2. You could use coroutines to hold up a function while waiting for some event. For example, you could make a WaitForKeyPress function which will infinitely loop until a key is pressed, then return, instead of dealing with states and event listeners which can get a little messy sometimes.
I’m sure there are many more uses, but those are the two that immediately popped into my head that I think would be extremely useful.
Thanks for the primer, TJ! Just one more reason i’m eager to dive in and rip a chunk out of this engine.
- Ryan
[...] Articles On Flash Design-DevelopmentNo tags Five Common AS3 to UnityScript Translations – untoldentertainment.com 09/25/2009 i’ve been using Unity3D on and off for a week now. [...]
Great post. The main reasons why I’m not in Unity are cost and, more importantly, I can’t draw in 3D, and already have a hard time with 2 dimensions. How are you guys getting your 3D models done?
Thanks, Brennon. As we mentioned in a recent post, our wonderful readers (maybe you?) helped us win a free copy of Unity core with Unity iPhone. Cost is definitely a concern when we go to buy a second copy, and then when we move to upgrade both suites to pro versions.
We haven’t had to draw anything yet, but i’ve poked around a little in Blender. Once my hair had turned completely white from the horrendous UI, i started to see how i could get some use out of it. You can also buy rights-free content packs of 3D models to suit your needs, which we may explore. Or we’ll hire a 3D artist – i haven’t decided yet.
- Ryan
[...] the rest at Untold Entertainment! Share and [...]
[...] Read the rest at Untold Entertainment! [...]
[...] Five Common AS3 to UnityScript translations [...]
[...] untoldentertainment.com » Five Common AS3 to UnityScript Translations [...]
[...] Five Common AS3 to UnityScript translations [...]
[...] Five Common AS3 to UnityScript translations [...]
[...] Five Common AS3 to UnityScript translations [...]