- Home /
Problem with destroying Transforms
I'm trying to make a very basic RTS level. The 'Enemy' is an object to make the player units move towards it and then destroys it when they touch it. The problem is that once they destroy it the game gives me an error "MissingReferenceException: The object of type 'transform' has been destroyed but you are still trying to access it". I tried having a backup copy of the Enemy object floating in the sky-box somewhere but then the player unit just floats up and goes after that, ignoring the Enemy objects that the player spawns.
How do I make the player unit sit there and wait for the player to spawn an Enemy object for it to move to instead of crashing instantly or chasing after ones thousands of miles away.
This is the script that the player unit is using and spawns on scene start.
var MoveSpeed : float = 2;
var Enemy : Transform;
var MaxDist = 10;
var MinDist = 5;
function Update ()
{
transform.LookAt(Enemy);
if(Vector3.Distance(transform.position,Enemy.position) >= MinDist)
{
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
}
}
function OnCollisionEnter(col : Collision) {
if(col.gameObject.tag == "Enemy_Goal") {
Application.LoadLevel("level2");
}
if(col.gameObject.tag == "Enemy") {
Destroy(col.gameObject);
}
}
Hey kringler, regarding your "$$anonymous$$issingReferenceException" error change Update function with following code.
function Update () { if(Enemy != null) { transform.LookAt(Enemy); if(Vector3.Distance(transform.position,Enemy.position) >= $$anonymous$$inDist) { transform.position += transform.forward*$$anonymous$$oveSpeed*Time.deltaTime; } } }
Answer by hamstar · Dec 09, 2013 at 06:28 PM
Is there just one enemy? Santosh gave the answer basically. You only want to follow the Enemy if the Game Object exists. As soon as you destroy the Enemy object there is nothing to follow.
You need to assign the Enemy transform reference again after destroying and respawning it. This could be done from another script or something like this:
var MoveSpeed : float = 2;
var Enemy : Transform;
var MaxDist = 10;
var MinDist = 5;
function Update ()
{
// see if enemy has been spawned if we don't have reference to it
if(Enemy == null) {
GameObject enemyObj = GameObject.FindWithTag("Enemy");
if(enemyObj != null) {
Enemy = enemyObj.transform;
}
}
// we already have a reference to the enemy
else {
transform.LookAt(Enemy);
if(Vector3.Distance(transform.position,Enemy.position) >= MinDist)
{
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
}
}
}
function OnCollisionEnter(col : Collision) {
if(col.gameObject.tag == "Enemy_Goal") {
Application.LoadLevel("level2");
}
if(col.gameObject.tag == "Enemy") {
Destroy(col.gameObject);
// make reference null so player stops trying to follow it
Enemy = null;
}
}
Your answer

Follow this Question
Related Questions
How to limit the angle of a camera with transform.RotateAround? 1 Answer
Area Effect Damage 2 Answers
Resources on how to make the player move/rotate in the camera direction? 1 Answer
Moving object with transform.position ignore other objects even if they collided 1 Answer
Weapon Bullet Question - Steadily increasing sine wave 1 Answer