- Home /
Move Game Object On Empty Game Object Different Each Time
I'm trying for some time now to create a simple script but my brain is not working as it seems. So let's say i have 3 cubes and 3 empty game objects. Each time i start i want it to give each cube a number from 1 to 3 randomly and then do the same for the empty game objects. So i assume using 2 randomizers is good. Next i want it to move the 3 cubes on the empty game objects position depending on the number each cube has with each empty game object. Here is a script that may help you understand better what I'm trying to achieve.
#pragma strict
function Start ()
{
// Select a number from 0 to 3 and give a number to each cube.
// Select a number from 0 to 3 and give a number to each empty game object.
// Also after a number is given to a cube or an empty game object i want it to remove that number to avoid giving the same number.
}
function Update ()
{
// The cube with id = 1 go to empty game object id = 2
// The cube with id = 3 go to empty game object id = 1
// The cube with id = 2 go to empty game object id = 3
}
Yes, exactly i want those 3 cubes to have a different number id and place them on the position of other game objects. So it's like a train. I want the train's wagon to change position each time the game starts. But how can i set a number id on a game object ?
Well, you define a public int or float property in a custom component script that is assigned to your gameobjects. You'll also need a "manager" script+object that will find those objects and assign values, change positions etc...
If you are beginning with scripting, I would advise you check all the material Unity's $$anonymous$$m provides (http://unity3d.com/learn/tutorials/modules/beginner/scripting), or other sources like tutorial videos on Youtube. There are also packages on the asset store that enable you to define objects behaviors with "visual scripting".
Isn't there a way to give a game object a number like:
var Cube1 : GameObject;
function Start ()
{
Cube1 = 1
//Or a random number like
Cube1 = Random.Range (0,3);
}
Answer by _dns_ · Nov 14, 2014 at 04:05 PM
Hi, I guess what you want is to "shuffle" those groups of objects. There is a quite simple algorithm to do this described here: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
One other way would be to give each of your object a number from 0 to 3 (any order), then assign each object a random number (using Random.value), and then sort the list using the random number. The list will then be randomized.
Your answer
![](https://koobas.hobune.stream/wayback/20220613171514im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can't figure out next step for this code ? 1 Answer
How to generate random objects? 1 Answer
random food spawning 2 Answers
Help with Generating Random Tiles 1 Answer