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
-1
Question by Tancre · Apr 04, 2015 at 08:37 PM · randomcube

Random cubes

so, i have 25 different-coloured cubes, all of the cubes have this c# script attached to them

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class RandomPosition : MonoBehaviour {
     public Vector3[] positions;
     void Start () {
     int randomNumber=Random.Range(0,positions.Length);
     transform.position = positions[randomNumber];
     
     }
     
 }

this script, every time i start the game, change the position of my cubes in one of the 25 positions that i made, the problem is that a lot of cubes go to the same position, and i don't want them to be in the same place. How do i do? please help me, and sorry for my bad english. :')

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Xarbrough · Apr 04, 2015 at 10:25 PM

Don't use 25 scripts. Use 1 script to manage all your objects, then just put them in a collection (array or list) to reference them.

Try this:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class RandomPosition : MonoBehaviour {
 
     //Both must be same length
     public GameObject[] cubes;
     public List<Vector3> positions = new List<Vector3>();
 
     void Start() {
 
         foreach (GameObject cube in cubes) {
 
             int randomIndex = Random.Range(0, positions.Count);
             cube.transform.position = positions[randomIndex];
             positions.RemoveAt(randomIndex);
         }
     }
 }


Instead of an array, use a list, so that every time you pick a position, you can remove it from the list (or make a copy first, if you want to maintain your array). This way you ensure, that a position can only be used once and the random range will always pick a number from within the items that are left.

Comment
Add comment · Show 5 · 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 Tancre · Apr 06, 2015 at 12:36 PM 0
Share

how do i make an array or a list? however after made this list i have to add this script to the list right? can you explain what i have to do a little bit better? I am a noob with unity :P. Anyway thanks for the help ;)

avatar image Xarbrough · Apr 06, 2015 at 01:40 PM 0
Share

You create an empty GameObject and attach my RandomPosition script to it. Then you drag all of your 25 cubes into the inspector field "cubes". That is the most easy version to get started. Next you will probably want to instantiate your cubes from a single prefab, or if you're already doing so, you would just have to to loop through your array and add them while instantiating. This is how it could work:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class RandomPosition : $$anonymous$$onoBehaviour {
 
     public GameObject cubePrefab;
     public int numberOfCubes;
     public List<Vector3> positions = new List<Vector3>();
     
     void Start() {
         
         for (int i = 0; i < numberOfCubes; i++) {
 
             int randomIndex = Random.Range(0, positions.Count);
             Instantiate(cubePrefab, positions[randomIndex], Quaternion.identity);
             positions.RemoveAt(randomIndex);
         }
     }
 }
avatar image Tancre · Apr 07, 2015 at 02:12 PM 0
Share

the console show to me this problem, when i apply to my gameobject the first script that you send to me:

Assets/RandomPosition1.cs(5,15): error CS0101: The namespace global::' already contains a definition for RandomPosition'

avatar image Xarbrough · Apr 07, 2015 at 05:26 PM 0
Share

There cannot be multiple script files with the same name, every class must be unique. Remove either your old RandomPosition script or rename the new one to RandomPositionNew until you can come up with a good name. You will have to change the name of the script file in the project, as well as the class name in the file itself.

avatar image kazandairesi · May 02, 2020 at 06:48 PM 0
Share

i have same problem and i try your script in my game but i dont understand why are all my cubes within together on 0,0,0 coordinates.

i really need help.

avatar image
0

Answer by hbalint1 · Apr 04, 2015 at 09:39 PM

Hello. Try changing the random seed, so it gives real random numbers every time. Put one of these to the first line of Start method:

 Random.seed = (int)System.DateTime.Now.Ticks;

or

 Random.seed = System.Environment.TickCount;
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 Tancre · Apr 06, 2015 at 12:38 PM 0
Share

i have tried it but it didn't resolve the problem :'(

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make an object/prefab choose between multiple given positions to spawn into? 1 Answer

Adding Imperfections? 1 Answer

Mapping Cube to Sphere - Using 64 planes as a cube "side" 1 Answer

How can I show a random integer on a cube 1 Answer

How can i destroy cubes that is randomly generated when i have passed them? 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