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 Tony_T · Nov 14, 2014 at 03:48 PM · randomgame objectid

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
 }
 

Comment
Add comment · Show 3
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 Tony_T · Nov 14, 2014 at 04:14 PM 0
Share

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 ?

avatar image _dns_ · Nov 14, 2014 at 04:27 PM 0
Share

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".

avatar image Tony_T · Nov 14, 2014 at 04:35 PM 0
Share

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);
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

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

28 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

Related Questions

Can't figure out next step for this code ? 1 Answer

How to generate random objects? 1 Answer

How do I randomize the movement & rotation of a 2D game object, such that it automatically moves in the direction it faces and not walk diagonally ? 3 Answers

random food spawning 2 Answers

Help with Generating Random Tiles 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