Problems assigning transforms on another script during Start().


So I’m having some trouble assigning a transform for multiple objects via a control script. For some background: basically what I am doing is created a grid of pieces. Each row is 50 pieces long, and as it is being created, each piece is stored in an array. Once the loop finishes creating and storing the pieces, I transfer the “currentPieces” array to the “previousCreatedPieces” array and then fill the “currentPieces” array in with a new row of pieces. And this works fine.

The next step is to assign a transform variable in a separate script for each one of the pieces with the code below. And the first code snippet works.

		for(var b = 1; b <= createdCount - 1; b++){			// Assigns partners for each piece in the middle rows
			var currentPiece : Transform;
			currentPiece = createdPieces**;	**

** var VectorGridMove : VectorGridMove = currentPiece.GetComponent(VectorGridMove);**
** VectorGridMove.connectPoint1 = createdPieces[b-1];**
__ VectorGridMove.connectPoint2 = previousCreatedPieces**;__
VectorGridMove.connectPoint3 = createdPieces[b+1];__
So for any piece, connect point 1 is the piece to the left in the same row, connect point 2 is the piece directly above which was in the previous row, and connect point 3 is the piece to the right in the same row. As you probably already guessed, I want to assign a 4 connection underneath, but since that row hasn’t been created yet I have to do something else.
I tried the following code (each set of two lines were tried separately) and I get the error “No appropriate version of ‘UnityEngine.Component.GetComponent’ for the argument list ‘(VectorGridMove)’ was found.”
for(var b = 1; b <= createdCount - 1; b++){ // Assigns partners for each piece in the middle rows**__

** var currentPiece : Transform;**
__ currentPiece = createdPieces**;
var VectorGridMove : VectorGridMove = currentPiece.GetComponent(VectorGridMove);
VectorGridMove.connectPoint1 = createdPieces[b-1];
VectorGridMove.connectPoint2 = previousCreatedPieces;
VectorGridMove.connectPoint3 = createdPieces[b+1];**__

__ VectorGridMove = previousCreatedPieces**.GetComponent(VectorGridMove); // error**
VectorGridMove.connectPoint4 = createdPieces**;**__

__ currentPiece = previousCreatedPieces**; // error**
** VectorGridMove = currentPiece.GetComponent(VectorGridMove);**
I’ve tried all kinds of combinations and searching through answers and forums and as far as I can tell this should work. I even rewrote my code to have three arrays, and I tried just assigning all four positions by accessing the middle array because I would have the row above and below stored, but that didn’t work either. So I’m hoping someone here can spot the mistake that I am making.__

I think there might be a problem that you’re using the component type as the variable name…

Try changing your variable to lowercase:

for(var b = 1; b <= createdCount - 1; b++){      // Assigns partners for each piece in the middle rows
	var currentPiece : Transform;
	currentPiece = createdPieces**;** 

** var vectorGridMove : VectorGridMove = currentPiece.gameObject.GetComponent(VectorGridMove);**
** vectorGridMove.connectPoint1 = createdPieces[b-1];**
__ vectorGridMove.connectPoint2 = previousCreatedPieces**;__
vectorGridMove.connectPoint3 = createdPieces[b+1];__
__ vectorGridMove = previousCreatedPieces.gameObject.GetComponent(VectorGridMove);
vectorGridMove.connectPoint4 = createdPieces;
currentPiece = previousCreatedPieces;
vectorGridMove = currentPiece.gameObject.GetComponent(VectorGridMove);

Also, make sure that every single piece game object actually has the VectorGridMove script attached to it.__

Solved!!! Although I still have a few questions. As seen above I still don’t get why I wasn’t able to reference the script a second time… maybe my solution has a secondary effect that I haven’t noticed, so if anyone comes by this and has any thoughts, please post them!

So what I thought of during the day and forgot about until after I put another 2 hours in screwing with this… is that my VectorGridMove script immediately jumps into update(). So it has three of the four transforms assigned and it tries to run and immediately explodes (figuratively). Even though I commented out all of the code and it was just running and doing nothing I couldn’t assign the fourth transform or find a way to reference the script.

What I did was turn the script off on the prefab, then assign all of the transforms and then enabled the script. This eliminates the need for a second reference because I added a third array and just work with the middle one as I described in my initial post. Everything now works the way I want and all connections (2500 pieces = 10,000 points) are assigned properly.

//for(var b = 1; b <= createdCount - 1; b++){			// Assigns partners for each piece in the middle rows

	//var currentPiece : Transform;
	//currentPiece = createdPieces**;	**

** //var vectorGridMove : VectorGridMove = currentPiece.gameObject.GetComponent(VectorGridMove); **
** //vectorGridMove.connectPoint1 = createdPieces[b-1];**
__ //vectorGridMove.connectPoint2 = previousCreatedPieces**;__
//vectorGridMove.connectPoint3 = createdPieces[b+1];__

if(w >= 2){

** createdCount = 50;**

** for(var g = 1; g <= createdCount - 2; g++){**

** currentPiece = previousCreatedPieces[g];**
** var script : VectorGridMove = currentPiece.gameObject.GetComponent(VectorGridMove);**
** script.connectPoint1 = previousCreatedPieces[g-1];**
** script.connectPoint2 = previousCreatedPieces2[g];**
** script.connectPoint3 = previousCreatedPieces[g+1];**
** script.connectPoint4 = createdPieces[g];**
** script.enabled = true;**
** }**
**} **