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 "MissingReferenceException" error change Update function with following code. function Update () { if(Enemy != null) { transform.LookAt(Enemy); if(Vector3.Distance(transform.position,Enemy.position) >= MinDist) { transform.position += transform.forwardMoveSpeedTime.deltaTime; } } }
– Santosh_Patil