• 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 Dexter_abhi77 · May 10, 2018 at 10:26 AM · positioning

How can i switch position of gameobject randomly but they should maintain a specific Position

I am making a stock exchange game where I can select stock to buy and sell them. I have listed the stocks as game objects line by line and made an array of those game objects in the script also their array of position. I want those game object to switch their places randomly when I hit play. for now, in the scene view stocks are placed serially but I want those to get jumbled but should stay one after another like I have placed in the scene view. please help me out. Here is my Scene viewalt text

Here is My code :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class filter : MonoBehaviour {
 
     public Dropdown Filter;
     public GameObject[] items;
 
     public Vector2[] positions;
     private float numberofitems = 4;
     int r;
 
     // Use this for initialization
     void Start () {
     
         positions = new Vector2 [numberofitems];
         
         positions [0] = new Vector2 (-283.5136f, 360.8717f );
         positions [1] = new Vector2 (-283.5136f, 345.8594f);
         positions [2] = new Vector2 (-283.5136f, 330.749f);
         positions [3] = new Vector2 (-283.5136f, 315.5405f);
 
         for (int i = 0; i < numberofitems; i++) {
             items [i].transform.position = positions [i];
             Debug.Log (items [r]);
         }
 
 }
     // Update is called once per frame
     void Update () {
         var r = Random.Range (0,numberofitems);
         items [r].transform.position = positions [r];
 
     }
 }
screenshot-19.png (433.1 kB)
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

Answer by tormentoarmagedoom · May 10, 2018 at 10:53 AM

Good day.

You only want to change the order of the Gameobjects?

Then i recommend you to create a new bool Array what we can call "PositionUsed". And do something like this:

 for (int i = 0; i < numberofitems; i++) 
  {
  int PositionToplaceTheObject = Random.Range(0,positions.length)
  while (PositionUsed[PositionToplaceTheObject] == true)
    {
    PositionToplaceTheObject = Random.Range(0,positions.length)
    }
  items [i].transform.position = positions [PositionToplaceTheObject]; 
  PositionUsed[PositionToplaceTheObject] = true;
  Debug.Log (items [r]);
  }

So now, before placing the object, you need to decide where it will go at randomly, but checkingin the while if that position has been taken. If is still empty, it goes outside the while and continue.


If helped, accept the answer

Bye :D!

Comment
Add comment · Show 7 · 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 Dexter_abhi77 · May 10, 2018 at 11:18 AM 0
Share

shall i write this in update or start ?

avatar image tormentoarmagedoom Dexter_abhi77 · May 10, 2018 at 11:23 AM 0
Share

Think!

I just "upgraded" you part of the code that places the gameobjects. So it must be at the start. IF all goes well, all ogbjects should be placed in a random position.

But as a recomendation: NEVER paste a code that is not writed by you, or that you dont understand what is doing.. it olny will give you more problems...

So... read my code, try to understand what is doing, and do you own code by yourself following this as a guide.


Bye! :D PD: Accept the answer so other people with same problem can easy find the solution :D

avatar image Dexter_abhi77 · May 10, 2018 at 11:30 AM 0
Share

Thank you for helping me out but while I am writing the code myself but it showing error while I am writing while inside for loop "PositionUsed[PositionToplaceTheObject] not taking

avatar image tormentoarmagedoom Dexter_abhi77 · May 10, 2018 at 11:34 AM 0
Share

Well, you need to declare the array! and initialize it! And when all objects are placed, you need to restart the array so all elements become false again!

avatar image Dexter_abhi77 · May 10, 2018 at 11:52 AM 0
Share

yes, I declared the array and wrote the code perfectly...not getting any error but it is not working.please help me out.

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     using UnityEngine.UI;
     using UnityEngine.Scene$$anonymous$$anagement;
     
     public class filter : $$anonymous$$onoBehaviour {
     
         public Dropdown Filter;
         public GameObject[] items;
     
         private Vector2[] positions;
         private int numberofitems = 4;
         int r;
         int PositionToplaceTheObject;
         public bool[] PositionUsed;
     
         // Use this for initialization
         void Start () {
     
     
     
             positions = new Vector2 [items.Length];
             
             positions [0] = new Vector2 (-283.5136f, 360.8717f);
             positions [1] = new Vector2 (-283.5136f, 345.8594f);
             positions [2] = new Vector2 (-283.5136f, 330.749f);
             positions [3] = new Vector2 (-283.5136f, 315.5405f);
     
             //items = GameObject.FindGameObjectsWithTag ("cement");
             for (int i = 0; i < numberofitems; i++) 
             {
                 while (PositionUsed [PositionToplaceTheObject] = true) 
                 {
                     int PositionToplaceTheObject = Random.Range (0, positions.Length);
                     PositionToplaceTheObject = Random.Range (0, positions.Length);
                 }
     
                 items [i].transform.position = positions[PositionToplaceTheObject];
                 PositionUsed [PositionToplaceTheObject] = true;
                 Debug.Log (items [r]);
     
             }
                     
     }    
         // Update is called once per frame
         void Update () {
             
     
     }
     
     }

 
avatar image Dexter_abhi77 · May 10, 2018 at 12:41 PM 0
Share

Hii, I wrote the code in the correct order but when I am playing it unity get hanged couldn't play the game get completely hang... I have to restart unity every time..Can you tell why is that happening ?

avatar image PizzaPie · May 10, 2018 at 12:50 PM 0
Share

That could take more time than expected as it guesses indexes so used indexes can and will come up repeatedly, more elements less chances to hit an empty index, of course it will eventually finish. Something like this would be better.

 void RandomizePositions()
     {
         List<GameObject> itemsList = new List<GameObject>();
         itemsList.AddRange(items);
 
         for(int i = 0; i < positions.Length; i++)
         {
             if (itemsList.Count == 0)
                 break;
 
             int index = Random.Range(0, itemsList.Count);
             itemsList[index].transform.position = positions[i];
             itemsList.RemoveAt(index);
         }


Overall I would use a Layout Group for positioning the elements ins$$anonymous$$d of hardcoded array of positions.

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

82 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

Related Questions

Random positioning of prefabs in memory game 1 Answer

Positioning imported armature with animation (from blender) 1 Answer

[Bolt] Minimap bug 1 Answer

Dynamically set a spawn point 3 Answers

Character position wrong on scene start 2 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