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 AndrewBrown123 · Aug 11, 2020 at 01:35 PM · gameobjectarrayvector3

Vector3 Position returns 0 when applied to GameObject

Hello

I have 2 arrays one holds GameObjects (NumberSpace) and the other holds Vectors3 (SpaceArray). I get the position and randomize the Vector3 array all fine but when I tell the GameObject to go to the new Vector3 position from the Vector3 array it returns at 0 or just dose nothing. The goal of this function is to change the position of the GameObjects via the order of the positions in the Vector3 array.

I've been looking for about 2 weeks now I'm giving up and asking for help.

Here is the code.

 void PositionChange()
     {
         SpaceArray = new Vector3[NumbersSpace.Length];//get the length...
         for (int i = 0; i < NumbersSpace.Length; i++)
         {
             //get the positions...
             SpaceArray[i] = NumbersSpace[i].transform.localPosition;
             //set the position...
             //this is were it stops working right...
             //NumbersSpace[i].transform.localPosition = SpaceArray[i];
             for (int j = 0; j < SpaceArray.Length; j++)
             {
                 NumbersSpace[i].transform.localPosition = SpaceArray[i];
                 /*NumbersSpace[i].transform.localPosition = SpaceArray[i];
                 NumbersSpace[0].transform.localPosition = SpaceArray[0];
                 NumbersSpace[1].transform.localPosition = SpaceArray[1];
                 NumbersSpace[2].transform.localPosition = SpaceArray[2];
                 NumbersSpace[3].transform.localPosition = SpaceArray[3];*/
             }
         }
     }

I call the function with a UI button else were in the script. I thank anyone who comes along and helps me with this problem. Thank You.

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

1 Reply

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

Answer by Bunny83 · Aug 11, 2020 at 03:15 PM

What is the point of this for loop?

 for (int j = 0; j < SpaceArray.Length; j++)

You just execute the exact same line several times. This is completely pointless. Note that the code inside your loop only uses the for loop variable i from the outer loop. Though I don't see any reason why you would iterate through all objects several times.


You said something about randomizing, but we can't really see anything that looks like randomizing.


If you really just want to randomly swap out positions of the gameobjects the general approach would be

 SpaceArray = new Vector3[NumbersSpace.Length];
 for (int i = 0; i < NumbersSpace.Length; i++)
 {
     SpaceArray[i] = NumbersSpace[i].transform.localPosition;
 }
 
 // randomize SpaceArray here
 //SpaceArray.Shuffle();
 
 for (int i = 0; i < NumbersSpace.Length; i++)
 {
     NumbersSpace[i].transform.localPosition = SpaceArray[i];
 }


To shuffle the array you could use an extension method like this one:

 public static class ArrayHelper
 {
     public static void Shuffle<T>(this T[] arr)
     {
         for (int i = arr.Length - 1; i > 0; i--)
         {
             int r = Random.Range(0, i + 1);
             T tmp = arr[i];
             arr[i] = arr[r];
             arr[r] = tmp;
        }
     }
 }

Which this class inside your project you can simply do:

 SpaceArray.ShuffleArray();

to shuffle the array.

Comment
Add comment · Show 1 · 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 AndrewBrown123 · Aug 11, 2020 at 04:29 PM 0
Share

Thank You so much for your help.

I had to rewrite my shuffle function but it work perfectly now.

Updated Code.

 public static void ShuffleObject<T>(T[] array)
     {
         int tmp = array.Length;
         for (int i = 0; i < tmp - 1; i++)
         {
             int tmpIndex = UnityEngine.Random.Range(0, i + 1);
             T t = array[tmpIndex];
             array[tmpIndex] = array[i];
             array[i] = t;
         }
     }

$$anonymous$$y PositionChange Function update.

     void PositionChange()
     {
         SpaceArray = new Vector3[NumbersSpace.Length];//get the length...
         for (int i = 0; i < NumbersSpace.Length; i++)
         {
             //get the positions...
             SpaceArray[i] = NumbersSpace[i].transform.localPosition;
         }
         ShuffleObject(SpaceArray);
         for (int i = 0; i < NumbersSpace.Length; i++)
         {
             //set the position...
             NumbersSpace[i].transform.localPosition = SpaceArray[i];
         }
     }


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

236 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

Obtaining an array of positions from an array of gameobjects 2 Answers

C# Array with random.range help? 2 Answers

Find gameobject furthest from player along z axis 0 Answers

What is the best way to instatiate an array of connected GameObjects? 0 Answers

XBox Controller Angle of Fire Incorrect 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