Trouble assigning transforms of two raycast hits to two different objects.

Hey,

In my scene I have six objects parented to an empty game object. I have two separate empty game objects. (Eventually there will be 20 or so empty game objects, with six child objects a piece.) What I am trying to do is assign the parent of a first object hit by a raycast to a variable and then assign the parent of a second object (with a different parent) to a second variable.

If a third object is selected I want to wrap around to the beginning, so the parent of a third selected object would be assigned to the first variable and the parent of the fourth object selected would be assigned to the second variable, etc.

The code I have below works to a certain degree. If I start the game and select one object, it works. When I try to click the second object, the first click assigns the parent of the first clicked object to the second variable. If I click it a second time, it is assigned correctly. Unfortunately this throws off my timing and counts with other scripts. I cant seem to clear the raycast or find another work around. I’m hoping someone can provide some insight.

static var selectFirst : GameObject;
static var selectFirstParent : GameObject;
static var selectSecond : GameObject;
static var selectSecondParent : GameObject;
static var clickCount : int;
var firstHit : RaycastHit;
var secondHit : RaycastHit;


function Update(){

    if(Input.GetMouseButtonDown(0) && collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), firstHit, Mathf.Infinity) && collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), secondHit, Mathf.Infinity)){
   
    clickCount += 1;
    FirstClick();
    SecondClick();
    	
	}
}


function FirstClick(){

		if (clickCount == 1){
    
		selectFirst = firstHit.collider.gameObject;
		selectFirstParent = selectFirst.transform.parent.gameObject;
		Debug.Log(clickCount);
		Debug.Log(selectFirstParent);
    	}
    	
    	else{}
	}
    	
    	
function SecondClick(){

		if (clickCount == 2){
    
		selectSecond = secondHit.collider.gameObject;
		selectSecondParent = selectSecond.transform.parent.gameObject;
		Debug.Log(clickCount);
		Debug.Log(selectFirstParent);
		clickCount = 0;
    	}

        else{}
}

There’s something weird with this code: you cast two identical rays, thus the same object will be hit by the two rays! Furthermore, you’re using collider.Raycast, which ignores all objects except the one to which the script is attached - is this script attached to each object?

If the script is attached to a single object (the camera or any other), you could use the following:

static var selectFirst : GameObject;
static var selectFirstParent : GameObject;
static var selectSecond : GameObject;
static var selectSecondParent : GameObject;
static var clickCount : int = 0;

function Update(){
  if (Input.GetMouseButtonDown(0)){ // if button pressed...
    var hit: RaycastHit; // cast a ray through the mouse position
    if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)){
      switch (clickCount){ // check the state:
        case 2: // after 2 clicks, restart from the first object
        case 0: // nothing clicked yet: save first object ref
          selectFirst = hit.collider.gameObject;
          selectFirstParent = selectFirst.transform.parent.gameObject;
          clickCount = 1;
          break;
        case 1: // 1st object already clicked: save the 2nd
          selectSecond = hit.collider.gameObject;
          selectSecondParent = selectSecond.transform.parent.gameObject;
          clickCount = 2;
          break;
      } 
    }
  }
}