• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by smnerat · May 24, 2013 at 03:23 AM · variablegetcomponent

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 t$$anonymous$$s 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 w$$anonymous$$ch 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 somet$$anonymous$$ng 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 searc$$anonymous$$ng through answers and forums and as far as I can tell t$$anonymous$$s 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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · May 24, 2013 at 03:34 AM 0
Share

How is GridMove class defined? In particular is it derived from Monobehaviour?

avatar image smnerat · May 24, 2013 at 03:41 AM 0
Share

Hmmm, I believe so? I thought I was just supposed to use the name of the script which is "VectorGridMove" no spaces. And its a javascript.

avatar image iwaldrop · May 24, 2013 at 04:23 AM 0
Share

Is VectorGridMove a CS class? If so, place the Type in quotes:

 currentPiece.GetComponent("VectorGridMove");

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.

avatar image smnerat · May 24, 2013 at 04:39 AM 0
Share

It's a javascript. I've tried doing that and it seemed to work to some degree. I could assign VectorGridMove 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.

avatar image MD_Reptile · May 24, 2013 at 05:31 AM 0
Share

i might be misreading this JS but dont you need like...

 var localNameOfMyScript : ActualNameOfThatScript = blah.blah;

???

then you would be like

 localNameOfMyScript.DoStuff();

or

 localNameOfMyScript.SomePublicVariable = someLocalVariable;


OR to be real specific:

 var myVectorGridMove : VectorGridMove = currentPiece.GetComponent(VectorGridMove);

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:

 myVectorGridMove.connectPoint1 = createdPieces[b-1];

perhaps none of this actually applies to your problem just putting that out there :D

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tomer-Barkan · May 24, 2013 at 05:45 AM

I t$$anonymous$$nk 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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image smnerat · May 24, 2013 at 01:40 PM 0
Share

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 vectorGridMove 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.

avatar image Tomer-Barkan · May 24, 2013 at 01:52 PM 0
Share

Something else comes to mind... what happens the first time this code runs(ie when previousCreatedPieces doesn't have anything since it's the first row)?

avatar image smnerat · May 24, 2013 at 01:59 PM 0
Share

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.

avatar image Tomer-Barkan · May 24, 2013 at 02:06 PM 0
Share

Ok one more thing - though it shouldn't make a difference. Create a new variable instead of overriding the old one. So change lines 9-10in my code (and remove the last 2 lines):

 var oldVector : VectorGridMove = previousCreatedPieces[b].gameObject.GetComponent(VectorGridMove);   
 oldVector .connectPoint4 = createdPieces[b];
               

If it still doesn't work I recommend using the debugger and tracing the variables. Let us know what happens.

avatar image smnerat · May 24, 2013 at 07:04 PM 0
Share

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.

Show more comments
avatar image
0

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 t$$anonymous$$s 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 t$$anonymous$$s.. 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 not$$anonymous$$ng 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. T$$anonymous$$s eliminates the need for a second reference because I added a t$$anonymous$$rd array and just work with the middle one as I described in my initial post. Everyt$$anonymous$$ng 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;
     }
 }        

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Any problems with this script? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can't Call function in another script 1 Answer

How am I supposed to use GetComponent? 1 Answer

Is it possible to use a variable as a Type? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges