0
\$\begingroup\$

I have a script that lets the player place blocks (like in Minecraft), but I also want to be able to destroy those blocks. I keep getting error messages cs1502 and cs1503.

Could anyone take a look at the script and tell me what is wrong?

using UnityEngine;
using System.Collections;

public class TestForCube : MonoBehaviour {

  Ray ray;
  RaycastHit hit;

  // Use this for initialization
  void Start () {
  }

  // Update is called once per frame
  void Update () {

    ray = Camera.main.ScreenPointToRay (Input.mousePosition);

    if (Physics.Raycast (ray, out hit)) {

      if (Input.GetKeyDown (KeyCode.Mouse0)) {

        Vector3 position = hit.transform.position + hit.normal ;
        Quaternion rotation =  Quaternion.FromToRotation( Vector3.up , hit.normal );
        GameObject Placement = GameObject.CreatePrimitive( PrimitiveType.Cube );
        Placement.transform.position = position;
        Placement.transform.rotation = rotation;
      }

      {
        if (Physics.Raycast (ray, out hit)) {
          if (Input.GetKeyDown (KeyCode.Mouse1)) {
            DestroyObject( PrimitiveType.Cube);
          }
        }
      }
    }
  }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ What lines are producing the errors? Did you search for it (cs1502, cs1503)? \$\endgroup\$ – Vaillancourt Mar 22 '18 at 15:06
2
\$\begingroup\$

The line that's giving you the errors is this:

DestroyObject(PrimitiveType.Cube);

The DestroyObject method requires an Object type parameter, but you're passing a UnityEngine.PrimitiveType enum type.

It's not completely clear what you want to achieve, but if you want to destroy a game object when it's clicked with the right mouse button, you should keep the references to the game objects you create when clicking the left mouse button and use Destroy instead of DestroyObject.

\$\endgroup\$
  • \$\begingroup\$ thank you, what do you mean by keeping the references? \$\endgroup\$ – eekskek Mar 22 '18 at 15:37
  • \$\begingroup\$ A reference is the GameObject Placement variable value that you used before in the code. That variable keeps a reference to the game object just instantiated, so that after you can manipulate that game object by using the Placement variable. That variable, though, is local in scope, it means that it will be destroyed as soon as you exit the {} of the if condition. So you need to have a class scope variable (or list of variables) of type GameObject that will keep the reference to the objects you create, in order to use them anywhere inside the class. \$\endgroup\$ – Galandil Mar 22 '18 at 15:41
  • \$\begingroup\$ hmmm, I see. Would I have to state the GameObject or something else as public? I tried that earlier but it didn't work. I have much to learn \$\endgroup\$ – eekskek Mar 22 '18 at 16:43
  • 1
    \$\begingroup\$ Before diving into Unity, I'd suggest you learn the basics of OOP and C#, like what are reference and value types variables, their scope, etc. You need those concepts in order to write even basic scripts. You should find a lot of material regarding basic concepts just by googling. \$\endgroup\$ – Galandil Mar 22 '18 at 17:30

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.