- Home /
update function problem
my problem is that i keep getting this error MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. i understand that one of my scripts has an update function and is still trying to find this object after it got destroyed ....how can i make the script stop running the update function after the object (cube) gets destroyed????or better yet how can i fix this error?? the script is.......... http://pastebin.com/X2UCPmYf the "question1" thing is the script that it opens once its close to the player
If the error is in this script you posted, then i suppose that the target is the object that is missing. Either this, or the error is in the "question1" script. Can you confirm it? Also, do you mean that the unity editor crashes when this error occur?
Answer by GuyTidhar · Jun 19, 2011 at 08:36 AM
Most likely, the reason for this is due to the use of GetComponenet within the Update() function. You should call the getComponent only once before starting the update (enable=true starts the script...). This way, you address any errors of this kind before doing updates and you avoid a bad performance, as the GetComponent function is an expensive one. Do something like this: in the scope of the class:
private var other;
void Start()
{
other = gameObject.GetComponent("question1");
}
then use other in update without the need to constantly call GetComponent.
Also, if you know in advance the type of the variable other - define it when you define the variable so you can catch errors before running your program (e.g. if "other" is a variable of type "GameObject" define it like this:
private var other : GameObject;
but where would i put the
if ( Vector3.Distance(target.position, transform.position ) < 5) { other.enabled = true; } if ( Vector3.Distance(target.position, transform.position ) > 5) { other.enabled = false;
You can keep it in the Update function. Generally speaking, try to put anything that does not change outside of the update function. When data changes and you need to do something to handle the change, you place it in the update.
so it would be like this???? http://pastebin.com/RJ6btqje
Answer by GuyTidhar · Jun 20, 2011 at 06:18 AM
It probably would be something like this, since I assume you wish the to change the state of the question1 component depending on the changing distance (doesn't the distance change in your game?)
var target : Transform;
private var other;
void Start()
{
other = gameObject.GetComponent("question1");
}
Update()
{
if ( Vector3.Distance(target.position, transform.position ) <= 5)
other.enabled = true;
else
other.enabled = false;
}
Also, notice that I used only one call to Vector3.Distance since you don't need the second one. Once you have the distance, using if...else you can decide what to do with it. Your distance is either bigger then 5, equals 5 or smaller then 5. If you'd want to do something else when it equals 5 you could still calculate the distance once and use the memory to test, like this:
update()
{
var distance : float = Vector3.Distance(target.position, transform.position );
if ( distance < 5 )
other.enabled = true;
else if ( distance > 5 )
other.enabled = false;
else // this means distance == 5 and you don't need to specify the condition now
{
// do something else if you wish
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612111954im_/https://answers.unity.com/themes/thub/images/avi.jpg)