Calling static functions thru scripts in C#

Listen i know how to call instance function thru scripts, i saw your thread, but i tried to call a class function but no luck…

//Good for instance functions

GameObject Amm= (GameObject) ( ( (FabricAmmunition) g.GetComponent(typeof (FabricAmmunition))).CreerAmmunition ());

//i tried this for class functions

GameObject Amm= (GameObject) ( ( typeof (FabricAmmunition) g.GetComponent(typeof (FabricAmmunition))).CreerAmmunition ());

I am having a hard time understanding those. Some questions:

  1. What is ‘g’?
  2. That script FabricAmmunition has a function called CreerAmmunition() that returns a GameObject?
  3. What errors are you getting

Usually when I see that much typecasting going on I get nervous. Have you tried breaking up each step into separate statements?

I’d agree with the amount of casting being a little excessive.

Your first example gets a component named “FabricAmmunition”, and casts it correctly, then calls a function within that script called “CreerAmmunition()”, which looks like it returns some type of object, and you are casting it to a “GameObject”, then storing that gameobject.

You second example however doesn’t make sense. You are essentially trying an incorrect cast by using the typeof keyword in front of the cast of the component returned by GetComponent().

Now your subject is about calling static functions, and neither of the examples have anything to do with static functions. If your CreerAmmunition() function is static, it needs to be declared like this.

public class FabricAmmunition
{
  public static GameObject CreerAmmunition()
  {
    // place your gameObject returning code here
    return gameObject;
  }
}

Then to call it, all you need to do is this:

GameObject Amm = FabricAmmunition.CreerAmmunition();

Note that this function isn’t available in any components of type FabricAmmunition, but is available as a static function from anywhere in your project. There are also some other rules about what types of variables you can use within your static function, along with a couple of other gotchas, so I’d recommend doing some research and getting to know more about the C# language. Here is a good place to start for the static keyword http://msdn.microsoft.com/en-us/library/98f28cdx(VS.71).aspx

EDIT : Looks like theres some issues posting the link, just copy and paste it in, or do a search for the static keyword on MSDN.

Good luck :slight_smile:

Damn thx now i see it, i’m so used to C++ ways by the ways. By the way,when i’m talking about an instance method or function it’s a non static method a class method by definition is a static method/function.

So my examples where if FabricAmmunition is an instance method. Plus i knew in C# it was class.method () for static but i did not know unity would recognize it without specifying the script. Thx alot!

P.S. g is a gameobject and i would like for C# to implement some casts like C++ static_cast. at least it would make the code clearer.

I believe there is a set of generic functions for C# coming in Unity 2.6, so there won’t be all this waste of time spent writing 2 casts in C# every time you want to use GetComponent().