static functions are class functions, not object functions (like static variables too) as such you access them through ClassName.StaticFunctionName as they are static and not related to a class instance, you can also only access static variables in them, not regular ones (as there is no object on which they are running)
Thanks but I don't understand...well.. This is FPS tutorial's enemy Robot's script, Code (csharp): static function CopyTransformsRecurse (src : Transform, dst : Transform) { dst.position = src.position; dst.rotation = src.rotation; for (var child : Transform in dst) { // Match the transform with the same name var curSrc = src.Find(child.name); if (curSrc) CopyTransformsRecurse(curSrc, child); } } Why here used static function?
In the case of the FPS tutorial/script/demo thingy, as you can see the transform of both the source and destination objects are passed in the signature of the function(method). There would be no reason to instantiate this as part of an object because you have the objects in question pass through the sign. With that said, there are times when you want methods/functions/classes available in memory from the beginning of runtime. You create these as static(great for helper/utility classes) so you can execute their respective code because they are "typically" always available. Now let me give you a stupid example: Code (csharp): // Static Class public static class MyClass { static MyNameMethod(string myName) { Console.WriteLine(string.format("My Name Is: {0}", myName); } } //Non Static Class/Method public class MyClass { MyNameMethod(string myName) { Console.WriteLine(string.format("My Name Is: {0}", myName); } } Now lets say you want to execute the MyNameMethod(function), using the static version you would do: Code (csharp): MyClass.MyNameMethod("Jimmy James"); None static would be: Code (csharp): MyClass myClassInstance = new MyClass(); myClassInstance.MyNameMethod("Jimmy James Don Dawn");
Static mean that you can access the data from anywhere (read global) The static function and variable have a global scope. You can access a static variable from any script. Variable that are not static are local to the current script file and cannot be accessed from another script.
This is not correct; static does NOT mean global (I have to emphasize this because it seems people get this wrong a lot). Dreamora already said what static means. If a variable is public, it is NOT local to that script, it can be accessed from anywhere (see this link which I've posted about 5 million times ). Do NOT use static variables as a way of "making them global"; you are likely to create problems for yourself. (See the 5 million topics where people ask "how come when I change this variable for one enemy, it changes for them all?" Answer: 'Cause you made it static.) To be perfectly clear, static has nothing to do with global. It's quite possible to have a private static variable, in which case it's local ("private static var foo = 5;"). Public = global, private = local. Static = static (neither global nor local). --Eric
Eric, that was beautiful. I don't know how many times I've told someone to remove the static keyword. I would have given a +1 on UA. We need to petition to have this page changed. Scroll down to "Global Variables" and start reading to see what I mean.
heh I was just asking my self witch class member are you than changing thanks Eric5h5 now I know private = local public = global static = global (but only 1 exists) am I right? well you say not private nor global private static = localized global (but only 1 exists) but if static limited with private to local it's localized global 1 but well if everybody can access it than it's global but if not more than 1 exists than it's still global but only 1 even if class correct me if I'm wrong it's kinda late again 6AM and usually I'm fuzzy if I'm tired. I'm asking my self why wouldn't it be global as it's same static == public static please correct me I want to know more about it and at space wisdom what takes less? I'm asking because static means only 1 and constant could mean more than 1 and let say example in class MonoBehaviour const static const and what's the difference between static class static function I see no difference as class is made for 1000 same classes if needed (with different inside) and function is always same inside as only 1 exists (except arrays) class and function can contain more functions and than static calss doesn't need constructor if only 1 exists
I want to learn how to use static functions but don´t know how they work: Lets say I have some boxes, they all share the same script with a boolean named "selected". If I click on a box its boolean gets true, while others booleans get false by using a static function. My script (remember its on all boxes) would look like: I don´t know how to call the static function on all boxes and also I can´t put the boolean into that function since its not static. Please help me (Javascript please) - thanks!
Anyone know if you can use static functions to manipulate the current object that the script is attached to? Right now the only way i know to do it is by making a static variable reference, for example: myScript.control.BlaFunction
No, you can't. Fundamentally, a static function is not attached to anything. The script might be attached to one object, to a hundred objects, or it might not be in the scene at all - and none of that matters a bit to a static function, because it's not attached to any of them. If you want an exception, search for information the singleton pattern. There's a ton of discussion on how to do it and whether or not it should be done, that I couldn't do justice to here. If you're not using a singleton, then merely asking that question suggests that the function should probably not be static at all.