- Home /
Creating Custom Script Classes
Until now I scripted based to a game-object-centered point of view, but clearly there must be something more outside of it!
I read that I can decleare new classes and access them after having stored them for example in a DLL file, not that I'm in need of a DLL for the moment, I just need to understand how to write a class unlinked from the game objects.
My object oriented programming stops at PHP 5 and a couple of Java months of work, so I'm a bit confused about how to replicate the same effect on Unity.
Where should I put my new code files, if they are not attached to any actual object in game?
How should I decleare new classes in those files?
Where can I tell other script that they have to recognize those new files with those new classes?
(I feel dumb just asking but I'm really unable to figure it out easily... >_>)
EDIT: For example I actually need to make out a script that reads informations from a game object, but I'm tired to duplicate the same function in every script I make, just to let every object be able to use it...
Answer by poncho · Apr 12, 2011 at 03:09 PM
1-those can be on the same project 2-if you want to access by instantiating a object of it, it would be public, if you just want to access it by just naming the class then use static 3-just need to make the build and save
hope it helps you
So are you saying that just putting the JS in the project assets list will include it in the recognized class list? Every script I put there? (BTW: Thanks for the answer!)
Answer by Extrakun · Apr 12, 2011 at 03:03 PM
Here's a sample JavaScript class
class foobar { private var x:int; public var y:string;
public void HelloWorld() { } }
Just create a new JS file called foobar (name of class has to match the class' name), and place it anywhere within your directory structure.
That's good for "How to decleare it", thanks a lot! But now, where should I put those code lines? And am I sure that in any other script, if I call foobar.HelloWorld() it will work, without placing it anywhere?