i want to clean up a code a little, and separate some functions that i will use through out the project. what is the best way to do it? to make empty game object and then the attach the script that holds the function? then i need to get the reference to that object and its script in order to use it? right?
how some of you are dealing with this matter. in time code gets bigger and it needs to be organized…
Hey bud,
No need to go such lengths. I created a folder specifically for my scripts. Then just drag and drop the scripts onto whichever object they are associated with.
I use a different folder than the one that comes in standard assets.
i ended up having some functions in camera script that really does not belong there, for example i have function that fades out certain objects, and “unfades” them…so i might need to reuse that function later on, so i shouldnt really need to have that function in camera script, i would like to separate it, do i need to have every script attached to something in order to use it?
The easiest way to do it is to use static functions. For instance, I have a Maps class that holds all of my mapping functions. I can call the static functions contained in my Maps class from any other script without needing a reference to an object. If you use static functions, you don’t need to have the script attached to a GameObject in the scene. However, for a fade out function you might want to have the script attached to something so you can make use of the Update function to do your fading.
hmm, I think I’m following what you’re laying down. No you don’t need every script attached to something to use that script.
A good example would be from the combat or AI tutorial. I have a script named patrolBot.js (which is attached to my enemy patrol character) that calls bulletScript which:
1: Instantiates a bullet,
2: then if hit player, Bulletscript will update the playerhealth script.
The PlayerHealth script is attached to the player.
If you will notice, bulletScript is attached to nothing, It’s called by reference in patrolBot.js
If your just looking for a place to store your code. Use a text document to keep notes on, and call it my notes(Or something) and just copy what you need from there. That way you can just copy and paste what you need, from there. If you change the language at the top of the script editor you can see your notes in the format you want to get references from. Then after it will auto revert to the text format. :idea: Thats what i do because most every one will expand there project creation lib way beyond the standard assets. (ie)I have at least 10 different examples to fade a light all small scripts so i put all the examples on the one text file to save space
Very cool! Is there any reason why you can’t have a static function which gets an instance of a class from the same file and returns the instance?
For example, let’s say I have a GUITools class inside a file that also provides a static function GetGUITools. Then from any other script I can call:
GUITools guiTools = GetGUITools()
?
I have tried using ScriptableObject but it can’t take arguments, which means no constructor in the class. A static function could either replace this completely, or if the above idea doesn’t work, it could at least call ScriptableObject the pass the arguments directly to properties to set up the object instance before returning it.
Am I making sense? Is any of this possible?
… how do you make a static function in C#. Isn’t everything still a class or can I make a function right in the *.cs file?
I tested using a static method to get an instance of a class from another file. It works perfectly (so far).
I created a file called GUILib (to hold GUI stuff I need, one of which is a class)
I added a static method which has the same arguments (signature) as my class constructor and returns an instance of the class.
For a simple example, lets call my class CustomButton and assume it takes an argument ‘label’ in the constructor. To call this from any script, I just type
The only tricky part was to find a MonoBehavior class that doesn’t throw warnings when using the ‘new’ keyword to instantiate (I’m talking about the class I instantiate in the static method, not the main class with the static method). It appears that the Behavior class works, which is the parent-class, or super-class, of MonoBehavior. I needed this because I needed to be able to use ‘transform’ and something else. VisualC# didn’t like it unless I inherited something with these names.
Does anyone know if this work-flow will cause unforeseen problems? It seems very easy to make support classes, functions and ‘global’ variables and constants this way!