Detect if object is a cube(find the primitive type) with physics.raycast

I am making a 3d puzzle game with picking up and placing cubes. I want to only be able to place a cube on a cube. I am using Physics.Raycast to determine where to place the cube. Is there a function I could use to determine the primitive type of the object hit with the Raycast?

You could use the mesh name to identify the primitive type - in this case, it’s “Cube Instance”:

...
var hit: RaycastHit;
if (Physics.Raycast(ray, hit)){
  var mesh: Mesh = hit.gameObject.GetComponent(MeshFilter).mesh;
  if (mesh.name == "Cube Instance"){
    // object hit is a primitive cube
  }
}
...

But this isn’t a good solution: the primitive mesh name could change in future Unity versions and make your game useless. A better (and faster) alternative would be to tag the cubes with a specific tag - like “Cube”, for instance:

...
var hit: RaycastHit;
if (Physics.Raycast(ray, hit)){
  if (hit.gameObject.CompareTag("Cube")){
    // object hit is tagged "Cube"
  }
}
...

NOTE: Remember to register the tags in the Tag/Layer Manager!

“Primitives” in Unity are really just arbitrary meshes, so they have no inherent value saying whether they are a cube, sphere, etc. You can write a script where you indicate the type yourself and attach it to the objects, such as

var primitiveType : PrimitiveType;

Then use GetComponent on the object hit with the raycast to read the primitiveType variable.