I have a prefab, and created two cubes from it. I need to differentaite between them in the script, how can I go about this?
I’m trying to create a “redirection” cube, so an incoming raycast that hits the cube creates a new raycast:
As you can see, I only want the laser to go through the cube being hit by the initial (pink) laser, but it goes through both since the script is attached to a prefab, and these objects are from that prefab.
Script:
public class redirectLaser1 : MonoBehaviour
{
public sourceLaser1 reference; //reference to class with the original line renderer
LineRenderer redirectLaser;
void Awake()
{
redirectLaser = GetComponent<LineRenderer>();
}
void Update()
{
RaycastHit redirectHit;
redirectLaser.SetPosition(0, transform.position);
//If cube detects raycast
if(reference.sourceHit.collider.tag == "Redirect")
{
if(Physics.Raycast(transform.position, -transform.forward, out redirectHit, Mathf.Infinity))
{
redirectLaser.enabled = true;
redirectLaser.SetPosition(1, redirectHit.point);
//If redirect hits another redirect
if(redirectHit.collider.tag == "Redirect")
{
redirectLaser.SetPosition(2, redirectHit.point);
}
}
}
else //If no collision, do not show laser
{
redirectLaser.enabled = false;
}
}
The error I get is in regards to this line:
redirectLaser.SetPosition(2, redirectHit.point);
The index is out of bounds.
So I ask, how can I refer to one cube and have the new line renderer (the blue one) only go through the cube that is being hit by the initial line renderer (pink)? How would I ignore the other cube object?
Is there a way I can differentiate between identical objects of the same prefab?