[SOLVED] Raycast name retrieval unexpectedly failing

Hello,
a bit of code for my raycasts aren’t working as expected:

       Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
       RaycastHit hit2;
       RaycastHit[] hit3;
       Debug.DrawRay(ray.origin, ray.direction * 50, new Color(255,0,0) ,0.1f,true);
            if (Physics.Raycast(ray, out hit2, 50f))
            {
                hit3 = Physics.RaycastAll(ray.origin, ray.direction, 50f);
                int count = 0;
                string[] name1 = new string[hit3.Length];
                foreach (RaycastHit i in hit3)
                {
                    name1[count] = i.collider.name;
                }
                Debug.Log("Object First: " + name1[0] + ", Last: " + name1[name1.Length-1] + ", Length: " + name1.Length);
                lookingAT = hit3[0].collider.gameObject;
                Debug.Log("Object that was hit: " + hit3[name1.Length-1].collider.name);
            }

weird additional info, ignore by going down to next highlighted text

The general gist is that a ray is created from the character looking outward to where the cursor is (cursor is locked to the middle of the screen.)

hit3[name1.Length-1].collider.name is the first object hit… I think?!. For the longest time, I thought the first object hit would be hit3[0] (code Debug.Log("Object First: " + name1[0] + ", Last: " + name1[name1.Length-1] + ", Length: " + name1.Length); still shows it… in other words its a whole load of bs :/)

When I look at the model, numbers start stacking like crazy on the last two lines of the console (“Object that was hit: Model” and “Object First: Model, Last: , Length: 2”). When I look at somewhere else, the top two lines start stacking numbers.

I don’t have a clue how the third line managed to happen. My guess is that something went haywire and its just utter rubbish. (Actually, maybe the raycast missed terrain and went straight into the sky???)

Anyways, I looked at the API for raycastall and I got the following helpful text regarding order: “Casts a ray through the scene and returns all hits. Note that order is not guaranteed.”

So anyways, just disregard anything about first and last, because that’s just rubbish now.

All that crap you see above is basically just additional information, so you can just ignore it if you don’t understand the ramblings.

My question is: In line 5 of the console, why is the “Last: ,” blank? The length tells us the raycast hit two objects and yet it can only get the collider name of the model. We all know that the other object can only conceivably be the terrain, yet it doesn’t show it.

Clearly, the problem doesn’t lay in hit*.*collider.name or the Physic*s.*raycast being unable to detect Terrain colliders since, in lines one and two, you clearly see Terrain.

Oh, one last thing: if you use the line Debug.Log("Object that was hit: " + hit2.collider.name);
you will get: Object that was hit: Terrain whether or not you are looking at the model, with the exception of looking into the air.

Am I going crazy?

Solution: add count++; in the last line of the foreach loop.

                int count = 0;
                string[] name1 = new string[hit3.Length];
                foreach (RaycastHit i in hit3)
                {
                    name1[count] = i.collider.name;
                }

That’s not going to accomplish anything other than repeatedly setting name1[0]. While you can technically use foreach, I’d suggest using a regular for loop, which makes errors like this harder to make.

–Eric

Oh my cheeseits, I didn’t add count++… I’m never using foreach again.

By the way, if you’re using latest version of unity, try the PhysicsRaycaster component and IPointerEnter interface, it works really well.