Ray question: Hit object is just a reference or a pointer to the actual transform?

Good day!

I just want to ask, because What I am doing here is if the ray hit an object I am iterating in the list for each object there, then i compare if it the actual object, then I assign some properties. Do I have to do this or I can just access the hit object from the ray?

RaycastHit hit;
                Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);

                if (Physics.Raycast(ray, out hit))
                {
                    Transform objectHit = hit.transform;
                    if (objectHit.tag == "rack_area" && !mouseOverGUI)
                    {
                        foreach (GameObject go in rackList)
                        {
                            if (go != null)
                            {
                                if (hit.transform.name == go.transform.name)
                                {
                                    DeselectRackLevel();
                                    go.GetComponent<MaterialHandler>().SetMaterial(1);
                                    tempName = go.transform.name;
                                    tempPosition = go.transform.position;
                                    tempRotation = go.transform.rotation;
                                    initializeAngle = true;
                                    spawnAreaSelected = true;
                                    rackSectionConfigWindowEnabled = false;
                                    rackBaseConfigWindowEnabled = true;
                                }
                                else
                                {
                                    go.GetComponent<MaterialHandler>().SetMaterial(0);
                                }
                            }
                        }

Ouch yeah you don’t need to do this!

hit.gameObject or hit.transform.gameObject should get you the object that was hit. Then you can do things like

MaterialHandler temp = ray.gameObject.getComponent();

if(temp != null)
temp.Setmaterial(1);//or whatever code you want on this object