I have multiple objects that are the same. Each one has its own script just carries code to detect raycast (prob should have had the raycast elsewhere, but not changing now). So when I click on an object does every single objects code get updated with what is in that raycast?
If so, it is possible for me to use the ‘this’ pointer in someway, so that only the raycast code from the actual object that was clicked is updated?
What do you mean by “Each one has its own script just carries code to detect raycast?”
You don’t really “detect a raycast,” a raycast is used to detect colliders (or mathematic planes).
Usually one takes a click, finds the ray-into-scene, raycasts and sees if you hit anything.
That’s like one script, probably on the camera or a “ClickManager” script, usually code like this:
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition);
if (Physics.Raycast( ray, out hit))
{
Debug.Log( "I hit " + hit.collider.name);
}
}
Ok so I have basically that code, but i store what the raycast gives into a gameobject, and i have that code on a script attached to multiple objects. But I want to check if what the gameobject is (that the raycast gave back) is the same as the game object i clicked.
So basically right now i am doing if(gameobjectFromRay = “string name”) but i dont want to do it by string name, since i have so many objects with the same name, i want a way to check that the gameobjectFromRay is the same object as the one i clicked, and not just another random object in the scene with the same name.
Things have gotten a bit confusing with my code so its a bit difficult to explain. I can try explaining again, if that doesnt make sense.
I’m just not following you. If you get a hit from the raycast, that already IS the GameObject that was hit (the one pointed to by hit.collider.gameObject). There’s no more checking that needs to happen. If you need to look up a script instance on that object to tell it to do something, such as “Hey you got clicked bro!” then use GetComponent on the thing you hit, if it has what you expect, go to town!