C# Help

I need help locating the termination function in this part of a script. Instead of deleting an object, I wan’t it to play an animation instead. Anyone know how to do this?

///<summary>
///reducescurrenthealthby'damage'pointsandkillstheobjectifhealthrunsout
///</summary>
publicvirtualvoidDamage(floatdamage)
{

if (!enabled)
return;

if (!vp_Utility.IsActive(gameObject))
return;

if (m_CurrentHealth <= 0.0f)
return;

m_CurrentHealth = Mathf.Min(m_CurrentHealth - damage, MaxHealth);

if (m_CurrentHealth <= 0.0f)
vp_Timer.In(UnityEngine.Rand
{
SendMessage("Die"); //pickedupbyvp_DamageHandlersandvp_Respawners
});


//TIP: ifyouwanttodothingslikeplayaspecialimpact
//sounduponeveryhit (butonlyiftheobjectsurvives)
//thisistheplace

}


///<summary>
///removestheobject, playsthedeatheffectandschedules
///arespawnifenabled, otherwisedestroystheobject
///</summary>
publicvirtualvoidDie()
{

if (!enabled || !vp_Utility.IsActive(gameObject))
return;

if (m_Audio != null)
{
m_Audio.pitch = Time.timeScale;
m_Audio.PlayOneShot(DeathSound);
}

RemoveBulletHoles();

vp_Utility.Activate(gameObject, false);

foreach (GameObjectoinDeathSpawnObjects)
{
if(o != null)
vp_Utility.Instantiate(o, transform.position, transform.rotation);
}

}
     
    }

looks like the “termination” is on line 15

vp_Utility.Activate(gameObject, false);

Sorry, I don’t quite understand line 15 says “return”. I have no idea how coding works in c# as I usually do it in javascript. Basically, I plan on simply deleting the termination code and substituting it for an animation instead.

First of all, I believe Cpt Chuckles wanted to say on line 51. And I agree with him, the “termination” code looks like is in the “vp_Utility”'s “Activate” method. We don’t have the code of “vp_Utility”, so you may want to go check it yourself.

Secondly, for the “return” you are asking about, it is just a way to skip the code after the “return”,
for example, on line 8 it means: if this script is not enabled, then return, which will skip the code after “return” in this entire method.
It’s being used here as a “guard”. if condition is not satisfied, skip the following code.

If you want to know the what’s happening behind “return”, this is not language specified thing, you may want to look for general programming&cs related materials.

1 Like

Awesome, Thanks!
One more thing, How would I destroy a gameobject that is parented to the object that has this script?

if you want to destroy(or do anything you want) a child’s parent,
you can access a gameobject’s parent by
transform.parent.gameobject

then, do your thing. (keep it nasty)

Ahh, great and how to access a gameobject’s child?

For all the hierarchy related things you can use the transform: Unity - Scripting API: Transform
Note that you can iterate the transform like a list of children.

So I entered the code below and got an error. Am I doing it right?
Destroy(transform.GetChild.Cam2);

It gives these three errors:
Expression denotes a method group', where a variable’, value' or type’ was expected
The best overloaded method match for UnityEngine.Object.Destroy(UnityEngine.Object)' has some invalid arguments Argument #1’ cannot convert object' expression to type UnityEngine.Object’

transform.GetChild (Cam2) works if Cam2 is an int (the index).
You can use transform.Find(“nameOfChild”) instead.