Making Object Clickable(Need Guidance ASAP)

So I’ve been learning alot of things from the forums about using Unity and coding certain aspects of the application that I’m developing.

Now, part of our application is to click on a building and make it load another screen then displaying information about that building.

I searched the forums and most answers point to Raycasting.

I’ve read here that it

calculates the direction of a line
corresponding to a screen position for
the given camera. This line, in the
form of a Ray object, can then be
passed to Physics.Raycast. This
function searches along the line from
its origin until it makes contact with
an object. The object returned by
Physics.Raycast (in the RaycastHit
parameter) is the object at the
original screen position.

I’ve

  1. downloaded his demo code and attached it to my First Person Controller.
  2. Set the target1 parameter to my test model(a small building made in google sketchup)
  3. Set the target2 parameter to my terrain.
  4. I’ve also put a box collider on my test model.
  5. I didn’t edit any of the code.

I click play and when I click on the Test model it prints “Hit Nothing” but when I click on the terrain,it prints “Hit Target 2”.

Can someone explain to me further how raycasting works?

What I know is that it returns the position and identifies the object where you “clicked” based on the distance calculated from the direction of the line corresponding to the screen position for the given camera…

I just need to be able to grasp this concept for me to get going in the development of our project.

I need to make the model be highlighted on hover and clickable and make it load another scene once it is clicked.

Is there any other alternatives on how to achieve what I want?

Please help me. Thank you in advance!

can’t you use like:

function OnMouseDown() {
      what ever happens when you click here
}

sorry made an awnser, i should have made it a comment like this.

Physics.Raycast checks if the logical ray passed as argument hits some collider. If so, it returns true and fills the RaycastHit structure with info about the object. The object must have a collider, or it won’t work. In your case, many things may be going wrong: the collider may be too small, or badly positioned, or something else is in front of it and blocking the building collider. You can change the script to a simpler thing like below, where the name of the clicked object will be returned - this may help you to understand what’s exactly going on:

function Update () {
	if (Input.GetMouseButton(0)) {
		var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		var hit: RaycastHit;
		
		if (Physics.Raycast(ray, hit)) {
			print("Hit "+hit.transform.name);
		} else {
			print("Hit nothing");
		}
	}
}

@Chesley suggested OnMouseDown, which may be a better solution in your case: when you must do always the same thing independently of the object clicked, use Raycast in a camera script; when you need each object to have a different reaction when clicked, use OnMouseDown in the object’s script:

function OnMouseDown(){
  print(name+" was clicked!");
}