trickCube go is a cube that is located way off the screen, just as a utility used to hold the originalColor and also serves as a temporary target.
When player approaches and faces the cube gameObject, it turns gray, as it’s marked to be destroyed when fire is pressed. When the player doesn’t press fire and just walks away from the cube, it should turn to it’s original color. When I press fire and destroy a cube, all seems fine, but when I approach another cube, I suddenly get this error:
MissingReferenceException: The object
of type ‘GameObject’ 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.
DmgCube_scr.MarkTarget () (at Assets/__scripts/DmgCube_scr.cs:48)
DmgCube_scr.Update () (at Assets/__scripts/DmgCube_scr.cs:38)
I know what that means, so please don’t bother explaining, just point out to me what am I missing in my code, where did I make the mistake.
using UnityEngine;
using System.Collections;
public class DmgCube_scr : MonoBehaviour {
Color originalColor;
GameObject target;
GameObject lastTarget;
GameController_scr gc_scr;
GameObject trickCube;
void Awake()
{
gc_scr = GameObject.Find ("GameController").GetComponent<GameController_scr>();
trickCube = GameObject.Find ("trickCube");
}
void Start () {
originalColor = trickCube.renderer.material.color;
lastTarget = trickCube;
}
void Update () {
// ::::::::::::::::::::::::::::::::::::::::::::::: TARGETING :::::::::::::::::::::::::::::::::::::::::::
Ray targetingRay = new Ray(transform.position, transform.TransformDirection (Vector3.forward));
RaycastHit targetHit;
if(Physics.Raycast (targetingRay, out targetHit, 1f))
{
if(targetHit.transform.gameObject.tag == "cube")
{
target = targetHit.transform.gameObject;
Debug.Log ("target = " + target.name);
//Debug.Log ("last target = " + lastTarget.name);
MarkTarget ();
}
}
}
void MarkTarget()
{
if(target != lastTarget)
{
lastTarget.renderer.material.color = originalColor;
target.renderer.material.color = Color.gray;
lastTarget = target;
}
}
// ::::::::::::::::::::::::::::::::::::::::::::::: ATTACKING :::::::::::::::::::::::::::::::::::::::::::
public void Attack()
{
if(gc_scr.dmgCharged)
{
Destroy (target);
gc_scr.dmgCharged = false;
lastTarget = trickCube;
}
}
}
the error happens in which line
– 767_2hi 7672 - I updated the error message.
– PaxForce