How to get first object in which Ray hit?

Hi, I have problem with get first object in which ray hit. I have two same objects in line. When i cast ray it come through first and second object and then return rayHit in the second collider. How can i get the first collider?

You can take the first collider by some different solution :

  1. When the raycasthit hit something, make the script executing the commands and destroy after (it would be shorter to don’t hit the second one).

  2. use something like this :

    var aGameObject : GameObject;
    var aNumber : int = 1;

    if(“I don’t remember the code for “if the raycasthit hit something” and I’m on phone so do this yourself” && aNumber == 1) {
    aGameObject = “the object you hit (I don’t remember the code to)”;
    aNumber = 2;
    }

and here’s other way to do it but I forget these while writing ^^
good luck with my bad envlish ^^
I hope it will help you

Thanks, but that don’t work. I have script:

...
Debug.DrawRay(pos4, (pos2 - pos4), Color.red, Vector3.Distance(pos4, pos2)); //for see raycast
        if (Physics.Raycast(pos4, (pos2 - pos4), out rayHit, Vector3.Distance(pos4, pos2))) //pos2 = actual position, pos4= position 2 frames ago
        {
        ...
        Debug.Log("HIT in " + rayHit.collider.name); // for see in what objects ray hit.
        ...
        }

And this script works good. But Raycast don’t work. I see the raycast is one frame too slow, but that i think doesn’t matter. The main problem is with hit detection:

First i see raycast hit in object in which i cast ray. That’s correct.

Then ray comes through plane, and this hit was detect. That’s correctt too.

But now: I have object with left and right collider (left and right armor):
First is right armor. Ray comes through and… don’t detect collision… I should see in console “HIT in Right armor” but there is nothing.
And then is left armor. And that collider is detected and script make some damage. But that damage should be in right side, not left.