0

I'm using UFPS in my FPS game. UFPS has a class vp_DamageHandler. It's attached to my target. Inside this class, there's:

public virtual void Die()
{
dieFunctionCalled = true; //the only thing I've added to this class, besides declaring dieFunctionCalled
...some UFPS code...//among other things, this is where the object is destroyed or deactivated
}

I want to keep the original files as clean as possible. I want to do my own coding in a different script to keep things tidy.

I want to do some other things when the target dies, so I've added myScript to my target as well, and tried this:

void Update () {
        if (GetComponent<vp_DamageHandler> ().dieFunctionCalled) {
            GetComponent<vp_DamageHandler> ().dieFunctionCalled = false;
            Debug.Log ("dieFunctionCalled");
        }
}

The problem is that the target is destroyed before the dieFunctionCalled is set to true. So the moment the target dies, myScript is deactivated and doesn't log dieFunctionCalled. If I would respawn the target, dieFunctionCalled does show up in the console.

I've tried attaching myScript to a empty GameObject, but then I get

Object reference not set to an instance of an object

Anyone who can help me with this? I'm new to Unity. Basically, all I want is to keep the original UFPS files as clean as possible, and write all my scripts in a different file.

binoculars
  • 2,226
  • 5
  • 33
  • 61

1 Answers1

0

I've tried attaching myScript to a empty GameObject, but then I get Object reference not set to an instance of an object

Create an empty GameObject called "DamageHandlerHolder" then Attach the script to it.

Find the Object that is holding the script

GameObject oObj = GameObject.Find("DamageHandlerHolder");

Get the script attached to it

vp_DamageHandler vpDm = oObj.GetComponent<vp_DamageHandler>();

Now you can check the variable:

if(vpDm.dieFunctionCalled){}

This solves your null problem but I suggest you use EventManager to register to events and be notified the player dies.

Programmer
  • 121,791
  • 22
  • 236
  • 328