Calling a static void function

Hi guys,
So I have a parent element which has a static void function. Once a certain condition is met, I want to call that function. From some reason I can’t.

The parent element (let’s call its class “parent”) has this:

static void PlayerDead

void PlayerDead()
    {
        Instantiate (Deatheffect, transform.position, Quaternion.identity);
        transform.position = OriginalPosition;
        gameObject.transform.eulerAngles = new Vector3 (0, 0, 0);
    }

The child element (class name “child”) tries to call the void function like this:

parent.PlayerDead();

However I am unable to call this function because it’s marked in red. What am I doing wrong?

The function is still undefined.

It shouldn’t even compile.

You need to define it:

static void PlayerDead()
{

}

Once defined you then should put code between the curly brackets… though of course that’s not needed to compile.

Every part of that definition means something.

The static declares it static, void is its return type, PlayerDead is its name, the () says it’s a function with 0 parameters, and the curlies define the body of the method.

First static void PlayerDead means nothing and actual function is void playerdead()
So you want delete first line and rename function to public static void insfead of just void. And yes it needs to be public, but i would suggest that you use public instead of public static as static functions have issues with non static. It makes work harder if you dont fully understand what you are doing

"public static void " gives me an error that

None of this helps, guys, I have on the parent:

public static void PlayerDead;
static void PlayerDead()
{
Destroy (Deatheffect);
Instantiate (Deatheffect, transform.position, Quaternion.identity);
transform.position = OriginalPosition;
gameObject.transform.eulerAngles = new Vector3 (0, 0, 0);
}

And on the child:

parent.PlayerDead();

I also get another error when trying to redefine it:

Assets/Scripts/parent.cs(38,21): error CS0102: The type parent' already contains a definition for PlayerDead’

You’ve misunderstood their instructions:

public static void PlayerDead()
{
    Destroy (Deatheffect);
    Instantiate (Deatheffect, transform.position, Quaternion.identity);
    transform.position = OriginalPosition;
    gameObject.transform.eulerAngles = new Vector3 (0, 0, 0);
}

This line right here:

public static void PlayerDead;

This doesn’t make sense. The format of the line implies its a ‘field’, but the field is typed as void, which it can’t be… you can’t have a void field. That’s why you get the error:

When defining a function you need parenthesis and a body to go with the name and type:

public static void PlayerDead()
{

}

In your example you have:

public static void PlayerDead;
static void PlayerDead()
{
    //some code
}

What you’ve done here is defined a public static FIELD named PlayerDead of type void (which doesn’t make sense to the compiler).

Then you defined a private non-static function called PlayerDead (duplicate name).
[correction] sorry, it’s static in this code I pasted here, but was reading the non-static version in the code in OP

I would say just do this:

public static void PlayerDead()
{
    //some code
}

But the code you have accesses non static members (primarily the transform of the objects).

What do you need this static for anyways?

I don’t think you mean non-static :face_with_spiral_eyes:

Though +1 for spotting the fact he’s got an object reference, I missed that :stuck_out_tongue:

Yeah, my mistake, though I did mean non-static…

I copy pasted the code in post 4, but read the code in post 1, and then mixed that with the public in post 4.

My apologies.