-1
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ @Gnemlock Oh, I totally forgot to show the console; I've updated the recording, showing the error. Could you go in detail what you mean with the initialisazion of Target? Othwerwise, the shown code is all I have currently \$\endgroup\$ – pguetschow Aug 25 '16 at 4:58
  • \$\begingroup\$ You have a 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 call DestroyParent(); however, in the code that you have supplied, you do not ever call that function. \$\endgroup\$ – Gnemlock Aug 25 '16 at 14:28
  • \$\begingroup\$ Even with the lack of information, I have tried simply supplementing the "call to 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\$ – Gnemlock Aug 25 '16 at 14:48
1
\$\begingroup\$

here you go, i made some edits to your code:

using UnityEngine;
using System.Collections;

public class MousePoint : MonoBehaviour {

RaycastHit hit;
public GameObject Target;
private float raycastLength = 500.0f;

void FixedUpdate () {    
    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;
                   //transform.name
                TargetObj.transform.name = "Target_Instance";
            }
        }
    }
    Debug.DrawRay(ray.origin, ray.direction * raycastLength, Color.yellow);
}
}

and second code:

as i understand this code is attached to gameObejct yo uwant do destroy, so..

  void DestroyParent () { 
     if(transform.parent.gameObject){
     Destroy(transform.parent.gameObject);
  }

}

\$\endgroup\$
  • \$\begingroup\$ Thanks for your effort, but unfortunately, this doesn't solve the problem \$\endgroup\$ – pguetschow Aug 27 '16 at 16:26
  • \$\begingroup\$ @TechTreeDev ok what if you first check, if it exists. wait i will update answer \$\endgroup\$ – Nick Aug 27 '16 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.