- Home /
When An Object Doesnt Exist, Help Making a Variable In Another Script Change
I need help making it so, if this object dies, set the variable "Target" on another script to the object with the tag "Player". This is the sliver of code.
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == "Zombie")
{
if (BarrCooldown == false)
{
health -= 1;
StartCooldown();
}
else if (health <= 0)
{
Destroy (parent.gameObject);
other.gameObject.GetComponent(AIFollow).Target = GameObject.FindWithTag("Player").transform;
}
}
}
if 'parent' is the parent of this game object (the one this script is attache to), then the fix might be as simple as reversing the order of lines 14 and 15. If you are destroying the parent, you are also destroying this game object, so the script never gets to line 15.
Did that then got this "NullReferenceException: Object reference not set to an instance of an object BarrScript.OnTriggerEnter (UnityEngine.Collider other) (at Assets/BarrScript.js:33)" I should also say, its now not destroying the parent.
Answer by meat5000 · Oct 26, 2013 at 02:15 PM
Two functions you may find useful
http://docs.unity3d.com/Documentation/ScriptReference/Transform.DetachChildren.html
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnDestroy.html
If I use OnDestroy, how would I get the variable 'other' which is the collider variable, to the script that has OnDestroy on it. Should I use GetComponant?
Use OnDestroy to perform a final operation on an object about to be destroyed. This function goes on the destroyed object itself. It's up to you how you implement this :) If you can reference the object to destroy it you can reference it to GetComponent a variable to/from it :) $$anonymous$$aybe OnDestroy can send out a required variable before the object is destroyed.
Detach children I personally found useful for making sure child objects don't get destroyed with the parent.
"BCE0019: 'gameObject' is not a member of 'Object'." Variable other is not defined until a trigger enters the object, no?
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == "Zombie")
{
if (BarrCooldown == false)
{
health -= 1;
StartCooldown();
}
else if (health <= 0)
{
Destroy (parent.gameObject);
}
}
}
function StartCooldown()
{
BarrCooldown = true;
yield WaitForSeconds(3);
BarrCooldown = false;
}
function OnDestroy()
{
other.gameObject.GetComponent(AIFollow).Target = GameObject.FindWithTag("Player").transform;
}
Declare a gameobject at the start of your script then in your collision function pass the other object into this variable. 'other' has limited scope as it was defined in a function but a variable declared in the class has scope class-wide.
If I do that I get "Assets/BarrScript.js(37,18): BCE0022: Cannot convert 'UnityEngine.Collider' to 'UnityEngine.GameObject'." Because the 'other : Collider' makes other a collider variable, and if I do other = other2, and have it as "other2.GetComponent(AIFollow).Target = GameObject.FindWithTag("Player").transform;"