Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 BenedictH · Dec 06, 2012 at 03:11 AM · arraysgameobjects

Problem in placing my array pieces

First my apologies for my noob qualities.

I am in the process of setting up the levels for the game I'm creating. All the GameObjects are set up in the script as a GameObject [] array called 'piece'. It is necessary to have 5 random pieces in position at the start of the game. I have been using Andeeeee's very useful Random generator that makes sure that the numbers are not repeated. Each piece has destinationX, destinationY and destinationZ variables, filled in the Inspector and the destinationPoints variable:

 var destinationPoints: Vector3;
 destinationPoints = Vector3(destinationX,destinationY,destinationZ);

...is used for the drag and drop when dragged by the mouse. Positioning the pieces without selecting them I'm using the if(piece[m] != null). Here's the function:

 function RandomiseSome()
 {
     var rangeTop = 16;
     var numSamples = 5;
     var l: int[] = RandFuncs.SampleRemove(rangeTop,numSamples);
     for(m = 0; m<15; m++)
     {
         if(m == l[0] || m == l[1] || m == l[2] || m == l[3] || m == l[4])
         {
             if(piece[m] != null)
             {
                 transform.position = (destinationPoints);
                 transform.rotation = Quaternion.Euler(rotationDestination);
             }
         }
     }
     MainMenuGUI.nivel = 0;
 }

I'm having a lot of problems with this and have tried it many different ways. Each time I get only one piece moving (always the same piece).

I have searched the forum for a long time to no avail. Any help would be greatly appreciated.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by PracticePad Inc. · Dec 07, 2012 at 12:48 AM

Ok, looks like a couple of things to consider.

The first step isn't a cure for the issue at hand (regarding the random pieces moving to the same place) but should help to eliminate redundancies that may make finding the problem more difficult.

The second step may or may not get the problem somewhere.

Make sure you back up your work first!

Step 1: If this script is on all of the pieces, and the pieces are being referenced by both tag and inspector, there are likely to be referential redundancies. A suggestion would be:

  • Create an empty gameObject in the scene and attach the script discussed above to it.

  • Keep the .FindGameObjectsWithTag() method in this script to reference the pieces, but don't assign pieces to this script in the Inspector by dragging (they are already referenced).

  • Remove the script above from each of the pieces.

Step 2: Put your Vector3 called 'destinationPoints' on each piece and then reference it, as follows:

  • Create a blank script, lets call it 'destination.js', with just the variable 'destinationPoints' and attach it to each of the pieces:

    public var destinationPoints: Vector3;

    • Be sure to fill in the destination points XYZ in the inspector for each piece. Don't assign these by script (they're already assigned in the inspector).

    • Now, go back to the script on the empty gameObject from Step 1 and substitute the following conditional (same one discussed previously):

      if (piece[m] != null) {

       var dest : destination = piece[m].GetComponent(destination);
           var destPoints : Vector3 = dest.destinationPoints;
       
           piece[m].transform.position = destPoints;
           piece[m].transform.rotation = Quaternion.Euler(rotationDestination);
           
       }
      

There are ways to potentially make this more efficient (e.g., declare vars outside of loops, etc.) but a good way to start is to make it work -- optimize later!

Comment
Add comment · Show 3 · 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 BenedictH · Dec 06, 2012 at 10:47 AM 0
Share

Thank you for your help PracticPad Inc.

In response to your question, the script is attached to all the pieces. I made the GameObject pieces as variables on the script and using: piece = GameObject.FindGameObjectsWithTag("Piece"); imported them. Then I dragged each of the GameObjects across to the appropriate place on the script's Inspector. I attached the script to all of the pieces. Then, selecting each of the GameObjects in turn, I went to the list of the GameObjects within the script and only dragged across that GameObject, leaving the rest blank - (None(Game Object)).

I tried your advice of having piece[m] at the beginning of:

 piece[m].transform.position = (destinationPoints);
 piece[m].transform.rotation = Quaternion.Euler(rotationDestination);

It was interesting what happened. All 5 of the random pieces moved which was great. But they all moved to the same place. I mentioned in my first post that only one piece had moved to it's correct place. Now all 5 pieces moved to that place (whether that part was one of the parts being moved or not). So the parts being moved is correct but the place it's being moved to seems to be locked on one position. Any ideas on getting them to go to the correct position?

avatar image BenedictH · Dec 06, 2012 at 11:04 AM 0
Share

I've discovered as well that the position it seems to be locked onto is that of piece[0].

avatar image PracticePad Inc. · Dec 07, 2012 at 12:55 AM 0
Share

btw - I haven't addressed the rotation in these snippets but the general idea is the same.

Also, I'm assu$$anonymous$$g that each of the pieces that can be chosen from 'knows' where it should go, as opposed to having 5 fixed places for the randomly selected pieces - this of course depends on how your game is designed to be played and affects what your solution ultimately will be.

avatar image
1

Answer by PracticePad Inc. · Dec 06, 2012 at 07:19 AM

Is the script attached to the only piece moving? If so, what about:

 if(piece[m] != null) {
 
     piece[m].transform.position = ... 
     piece[m].transform.rotation = ...  
 
 }

Otherwise the transform.position and transform.rotation have an implicit 'this' reference and will only affect the transform of the gameObject that the script is attached to.

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
avatar image
0

Answer by BenedictH · Dec 10, 2012 at 12:47 PM

Many thanks PracticePad Inc.! It works!!!

I spent some time going through the scripts to follow your advice, and cutting down on the amount of code relevant to what you were telling me. There is a script called DragDrop (for that very purpose) and I used that as the script with the destinationPoints. I got rid of the destinationX, destinationY, destinationZ floats as well as the destinationPoints = Vector3(destinationX,destinationY,destinationZ); as, as you stated, it is not necessary as the X, Y and Z of destinationPoints appears on the Inspector anyway (where I entered the destination X, Y and Z of each piece). I detached all except for the DragDrop scripts from the GameObjects, and attached the script to an empty GameObject.

In answer to your question, yes, the pieces that can be chosen know where they should go, so that's fine. I applied it to the rotation in the same way:

 var rotation : DragDrop = piece[m].GetComponent(DragDrop);
 var rotationDest : Vector3 = rotation.rotationDestination; 
 piece[m].transform.rotation = Quaternion.Euler(rotationDest);

and it worked too.

I do refer in the script in two different functions (one Update) to the destPosition and rotationDest so I've put the lines into each function and it works fine.

Many thanks!!!

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.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

How to display gameobjects when i pause the game? 1 Answer

objects in the same array wont move 0 Answers

Get a GameObject from a component 1 Answer

Find one inactive player (gameobject) 2 Answers

Multiple boards 0 Answers


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