For a small RTS Project, I'm working on the pointer. I want a pyramid as a marker on the terrain, that indicates the selected position, sinks into the ground and disappears. For the next interaction there should be another object that can be animated and so on.
Here's my C# code:
This handles the click event on the terrain:
using UnityEngine;
using System.Collections;
public class MousePoint : MonoBehaviour {
RaycastHit hit;
public GameObject Target;
private float raycastLength = 500;
void Update () {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, raycastLength)) {
Debug.Log(hit.collider.name);
if(hit.collider.name == "Terrain") {
if (Input.GetMouseButton(1)) {
GameObject TargetObj = Instantiate(Target, hit.point,
Quaternion.identity) as GameObject;
TargetObj.name = "Target Instance";
}
}
}
Debug.DrawRay(ray.origin, ray.direction * raycastLength, Color.yellow);
}
}
This should destroy the pointer object:
using UnityEngine;
using System.Collections;
public class DestroyParentGameObject : MonoBehaviour {
void DestroyParent () {
Destroy(this.gameObject.transform.parent.gameObject);
}
}
In my hierarchy, I have a Terrain
, a Camera
, a SpotLight
and a GameObject
"Target
" with the child "Pointer
". The target should be cloned.
At the end of the sink animation, I call DestoryParent()
from The script above. Unfortunately, it deletes the first existing object, and returns 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.
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineObjectBindings.gen.cs:63)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:80)
MousePoint.Update () (at Assets/scripts/MousePoint.cs:18)
The cloned object is a prefab with an empty Object
, with a Cylinder
as the child. The short code is bound to this cylinder; a placeholder for the arrow, later. The large code is appended to a global empty object that handles all world scripts
You can see what happens via my screen recording. Does somebody has an Idea how to solve this?
public Target
in your script, which your instantiating. Do you pass this in via editor, or do you get the object in another way? You also say you have the error when you callDestroyParent()
; however, in the code that you have supplied, you do not ever call that function. \$\endgroup\$DestroyParent
on end of animation" for a simple timer-based call, recreating the rest of your project as you describe with the same scripts. As it is, I do not get any error codes. Debugging questions must contain "a minimal, complete, verifiable example of the issue so that readers can diagnose it without needing to guess, read all of your code, or engage in extensive back-and-forth dialog" to be on topic, and as such, I have flagged this question for closure. \$\endgroup\$