- Home /
Problems assigning transforms on another script during Start().
Hey,
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[b];
var VectorGridMove : VectorGridMove = currentPiece.GetComponent(VectorGridMove);
VectorGridMove.connectPoint1 = createdPieces[b-1];
VectorGridMove.connectPoint2 = previousCreatedPieces[b];
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[b];
var VectorGridMove : VectorGridMove = currentPiece.GetComponent(VectorGridMove);
VectorGridMove.connectPoint1 = createdPieces[b-1];
VectorGridMove.connectPoint2 = previousCreatedPieces[b];
VectorGridMove.connectPoint3 = createdPieces[b+1];
VectorGridMove = previousCreatedPieces[b].GetComponent(VectorGridMove); // error
VectorGridMove.connectPoint4 = createdPieces[b];
currentPiece = previousCreatedPieces[b]; // 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.
How is Grid$$anonymous$$ove class defined? In particular is it derived from $$anonymous$$onobehaviour?
Hmmm, I believe so? I thought I was just supposed to use the name of the script which is "VectorGrid$$anonymous$$ove" no spaces. And its a javascript.
Is VectorGrid$$anonymous$$ove a CS class? If so, place the Type in quotes:
currentPiece.GetComponent("VectorGrid$$anonymous$$ove");
But you should really think of a way to cache the Components when you build the grid, because doing a GetComponent is pretty expensive. If you're doing it a lot you'll see a performance gain if you cache them.
It's a javascript. I've tried doing that and it seemed to work to some degree. I could assign VectorGrid$$anonymous$$ove using the line even in a javascript. What happened though is that all of the previous lines where I assigned connectPoint1,2,3 seemed to be undone. I kept getting the error "variable connectPoint1 has not been assigned, maybe you forgot to assign the variable in the inspector" or something along those lines.
i might be misreading this JS but dont you need like...
var localNameOf$$anonymous$$yScript : ActualNameOfThatScript = blah.blah;
???
then you would be like
localNameOf$$anonymous$$yScript.DoStuff();
or
localNameOf$$anonymous$$yScript.SomePublicVariable = someLocalVariable;
OR to be real specific:
var myVectorGrid$$anonymous$$ove : VectorGrid$$anonymous$$ove = currentPiece.GetComponent(VectorGrid$$anonymous$$ove);
because its "var" then "local name of that var" then : then "the TYPE of this var" in JS right???
and if im correct about that lol...
the rest would be:
myVectorGrid$$anonymous$$ove.connectPoint1 = createdPieces[b-1];
perhaps none of this actually applies to your problem just putting that out there :D
Answer by Tomer-Barkan · May 24, 2013 at 05:45 AM
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[b];
var vectorGridMove : VectorGridMove = currentPiece.gameObject.GetComponent(VectorGridMove);
vectorGridMove.connectPoint1 = createdPieces[b-1];
vectorGridMove.connectPoint2 = previousCreatedPieces[b];
vectorGridMove.connectPoint3 = createdPieces[b+1];
vectorGridMove = previousCreatedPieces[b].gameObject.GetComponent(VectorGridMove);
vectorGridMove.connectPoint4 = createdPieces[b];
currentPiece = previousCreatedPieces[b];
vectorGridMove = currentPiece.gameObject.GetComponent(VectorGridMove);
}
Also, make sure that every single piece game object actually has the VectorGridMove script attached to it.
See this is what is starting to aggravate me about this, because I've tried this before (and again double checking what I have matches with what you provided) and it still doesn't work.
Using either one of the methods I can assign vectorGrid$$anonymous$$ove with the second line commented out, but when I try to assign connectPoint4, I get the error "Object reference not set to an instance of an object".
All of the objects, are created from a prefab, and they behave properly when assigning the first three connections and I don't try and switch back to assign a fourth.
Something else comes to $$anonymous$$d... what happens the first time this code runs(ie when previousCreatedPieces doesn't have anything since it's the first row)?
I create an initial border row with pieces that don't interact with the other cubes and I store that as the currentPieces array so when the for{} starts I have something in both arrays. Then each subsequent row is created with a border piece, rowlength-2 of the normal pieces, then a border piece.
Ok one more thing - though it shouldn't make a difference. Create a new variable ins$$anonymous$$d of overriding the old one. So change lines 9-10in my code (and remove the last 2 lines):
var oldVector : VectorGrid$$anonymous$$ove = previousCreatedPieces[b].gameObject.GetComponent(VectorGrid$$anonymous$$ove);
oldVector .connectPoint4 = createdPieces[b];
If it still doesn't work I recommend using the debugger and tracing the variables. Let us know what happens.
I tried something along those lines last night to no avail. I will give it another try tonight. It seems like I'm creating a new instance of the vectorgidmove script or resetting it some how when I try to access it a second time.
Answer by smnerat · May 25, 2013 at 03:12 AM
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[b];
//var vectorGridMove : VectorGridMove = currentPiece.gameObject.GetComponent(VectorGridMove);
//vectorGridMove.connectPoint1 = createdPieces[b-1];
//vectorGridMove.connectPoint2 = previousCreatedPieces[b];
//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;
}
}