How do I make it know what it's hitting?

using System;
using UnityEngine;
class LeftClick : MonoBehaviour
{
static void Click(){
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, out hit, 100))
Application.LoadLevel(“Barracks”);
return;
}
}
}
}

This is what I have, but how do I make it know that it’s gonna hit my GameObject called Main Frame? Do I need to insert a tag to this gameObject? a script to it? I’m new to this… Thanks!

search the net for hit.gameObject for related questions

Try this:

if(hit.gameObject.tag == "EnemyBullit") { PlayerStats.HEALTH -= 20;}

And also hit.gameObject.name ==

print(hit.gameObject.name)

To hit an object with a raycast, it needs a collider. To add a collider to an object:

  • Select object
  • Click ‘Add Component’ in the inspector
  • Click on ‘Physics’
  • Select any of the colliders (mesh, box, sphere, capsule, etc).

In your code you will need to check if you have hit the right object. This can be done by comparing names (and many other ways). Eg:

    using UnityEngine;
    using System.Collections;

    public class LeftClick : MonoBehaviour 
    {
        void Update()
        {
            if(Input.GetMouseButtonDown(0))
            {
                RaycastHit hit;
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out hit, 100))
                {
                    if(hit.collider.name == "Main Frame")
                        Application.LoadLevel("Barracks");
                }
            }
        }
    }

The script needs to be attached to a game object in the scene in order for Update to be called.

  • First, go to the object that you want the ray to check if that’s the object it hit
  • When you have that object selected, look in the inspector at the very top
  • Where it says tag, click on the “untagged” and a list should drop
  • At the very bottom of that list, click on “Add tag”
  • Expand tags and increase the “Size” by one
  • Once you increased the size , a new element should of been added
  • Click on that element and give it the name you want, let’s say you called it “target”
  • Go back to your object and on the tag drop down choose the name you gave the element

Now we can do this:

public class LeftClick : MonoBehaviour 
    {
        void Update()
        {
            if(Input.GetMouseButtonDown(0))
            {
                RaycastHit hit;
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out hit, 100))
                {
                   if(hit.collider.gameObject.tag == "target")//substitute "target" for your tag name
                        Application.LoadLevel("Barracks");
                }
            }
        }

So what that does is, it checks to see if whatever the ray hit’s tag is target. If you assigned the tag “target” to a gameObject; when the ray hits it, it’ll execute the code.