Instantiated prefab references point to first prefab instantiated

I have a loop that instantiates ship objects for a rts game im making. I have a selector script that selects references of objects based upon a raycast hit operation. When i have one ship (without instantiating others) my code for selecting works fine, and so does deselecting code(i loop through all ship tagged objects and see if they have the selection obj attached to them) but when i just instantiate a few more ship prefabs, my code breaks.

I touch any ship and it selects the first ship instantiated, not the one i touch or my original one in the scene. I use a debug string to output the obj name selected, and if i tap the original (which is named MainShip1) it shows the correct name of the object but selects a prefab instantiation rather than the ship hit by the raycast. If i tap any prefab instantiation it selects the first prefab aswell that is instantiated but shows the name “MainShip1(Clone)” which is what it would under instantiation of a prefab.

Here are images of my scene and problem (notice the debug output too in the upper left):

This is when i select my ship without other prefabs instantiating:
Selecting prefab ship
Here is deselecting:
Deselecting one ship

Here is before i select a ship when i have other prefabs instantiated:
before selecting ship with other prefabs instantiated
Here is after i select a ship (The one to the left of the white cube which is my original and the selected ship is the first prefab instantiated)
selecting any ship with prefabs instantiated

Here is my instantiation code:

	Ship1 = (GameObject)Resources.Load("MainShip1");
	for(int i = 0;i<8;i++)
		GameObject ship = (GameObject)Instantiate(Ship1, new Vector3(-1435.566F + (i*7F),0,-1427.19F), new Quaternion(0,0,0,0) );
		ship.tag = "Ships";
		GameObject hull = ship.transform.Find("ShipHull").gameObject;
		ShipVars shipVars = hull.GetComponent("ShipVars") as ShipVars;
		shipVars.currentCommand = 0;
		debugGText.permDebug("ship number " + i + " is named " + + "and hullObj is named: " + + " and is on layer " + ship.layer + "and has tag " + ship.tag );

Here is my code for my raycast.hit code:

	 		if ((hit.collider) && hit.collider.gameObject.transform.parent.gameObject.CompareTag("Ships")) //if my ray hit a "Ship" tagged object (collider is component and this references the object it is attached to?
//change to raycast all, as it stores an array of all objects hit and ray doesnt stop at first one	
//or disable invisPlane collider until raycast finishes, then if no other colliders are hit reenable invisPlane collider and reraycast to determine move point.				
					//debugGText.permDebug("selecting ship from touch");
					debugGText.permDebug("hitObj name: " +;
					selectThisObj(hit.transform.gameObject); //references hull


And here is my selectThisObj method:

public void selectThisObj(GameObject g)
	g = GameObject.Find("ShipHull");
	//debugGText.permDebug("insideSelObjMethod, gameObj name is: " +;
	ShipVars script = g.GetComponent("ShipVars") as ShipVars;
	//debugGText.setDebug("after ShipVars referencing....");
	if(script != null)
		//debugGText.setDebug("script isnt null");
		if(script.isSelected == false)
			//debugGText.permDebug("SelectThis obj: script != null, isSelected var = f, selecting....");
			script.isSelected = true;
			//create selected obj here and parent to selected obj ship
			GameObject selectedObjGui = (GameObject)Instantiate(guiSel, g.transform.position, g.transform.rotation); //instantiate a prefab of my selection object at the colliders current coordinates								
			script.selGuiObj = selectedObjGui;
			debugGText.permDebug("inSelectObj method and attaching selObj to: " + + " and shipObj is " +;
			selectedObjGui.transform.parent = g.collider.transform;
			selectedObjGui.transform.localPosition =;
			selectedObjGui.transform.localRotation = Quaternion.identity;
			biggestDimension = script.biggestDimension;
			selectedObjGui.transform.localScale = new Vector3((float)(biggestDimension/3.0F), (float)(biggestDimension/3.0F), 1.0F);
		//debugGText.setDebug("script is null");
	somethingSelected = true;
	//debugGText.permDebug("calling populate list");

Here is my ship prefab hierarchy:


Well i hope i didn’t scare anybody away with providing code and images. I just am super confuse don how to debug this and why this would happen… :confused:

Hmm ok great find though, i was meant to find the hull of the parameter gameobject.
But when i change it to g.gameObject.Find(“ShipHull”); it complains giving this error:

Static member `UnityEngine.GameObject.Find(string)’ cannot be accessed with an instance reference, qualify it with a type name instead

This method exists in the gameObject type, and takes a string as its parameter. Why would it be asking for a type? Is it a generic method?

isnt using the var keyword without a type intensive and could lead to type unsafe operations? I always try to declare my types so i make sure to be aware if it references the transform or gameObject.

I found the answer, i was modifying the arrayList i was iterating over.
Here is my other post about it in case others have a similar problem:
Other Post Link

I solved it by doing:

//deselect first element