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 Gascon · Jan 13, 2015 at 10:41 AM · c#randomvalue

How to assign random int values to gameobjects

Hey guys, I am pretty new to C# and game programming in general and I have this question trickering me, I know this is probably the wrong way to approach this problem but here is my problem

Basically I have a randomID = Random.Range (1, 12); that I use on Awake() to assign a value to multiples objects when I start up the game to use that later on to access inventory content later on in my progress.

Now I have no idea how to assign a randomized value to multiple gameObjects and for them to keep that assigned value forever...The only thing that works is that both my objects have the same value right at the start! I use a bool to trigger their selected values to show up OnGUI whenever theres a OnTriggerEnter / Exit so I can see their different assigned values.

Comment
Add comment · Show 1
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 tanoshimi · Jan 13, 2015 at 11:47 AM 0
Share

Assu$$anonymous$$g each object makes its own call to Random.Range, that should be fine - of course there is a 1/11 chance that two objects will share the same value. If that's not what you're expecting, please post the actual code you're using.

2 Replies

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

Answer by InvincibleCat · Jan 13, 2015 at 11:50 PM

You also can use gameobject.GetInstanceID() so you will have a uniq ID without effort ;)

Cheers

Comment
Add comment · Show 3 · 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 Redwolve · Jan 14, 2015 at 12:00 AM 0
Share

This is one of my favorite parts of program$$anonymous$$g, there's never one right answer. I also love this forum because I get to learn very helpful things. I like this a lot it definitely has its uses.

$$anonymous$$y question would be referencing it later in code if it is as random as the instance ID. What value does it return, numberwise that is.

avatar image InvincibleCat · Jan 14, 2015 at 12:08 AM 1
Share

Not sure to understand your question but GetInstanceID() returns an int. It is not a random value but is still a unique value. And as @Gascon is looking for unique ID's this works ;)

avatar image Gascon · Jan 16, 2015 at 11:05 PM 0
Share

That's exactly what I want, its like 2 hits 1 stone O:

avatar image
0

Answer by Redwolve · Jan 13, 2015 at 11:32 PM

If your goal is to items and assign them their own not just random but also unique ID, I would recommend using a generic List.

add each ID 1-11, to that list. (I use uJS so you may have to look up the specifics)

I actually do this myself in a card matching game to set IDs for each card pair.

 var aRobotParts:List.<int> = new List.<int>();
         aRobotParts.Add(1);
         aRobotParts.Add(2);
         aRobotParts.Add(3);    
         aRobotParts.Add(4);
         aRobotParts.Add(5);
         aRobotParts.Add(6);    
         aRobotParts.Add(7);
         aRobotParts.Add(8);
         
     for(var i:int = 0; i<totalRobots; i++)
     {    
         
         for(var j:int = 0; j<1; j++)
         {
             var someNum:int = Random.Range(0, aRobotParts.Count);
             var animalNumber:int = aRobotParts[someNum];
             
             aRobotParts.RemoveAt(someNum);


What this does is generate a List of all possible IDs, and as it assigns that ID to a card it then removes that ID from the list. Leaving only the IDs that you have not already assigned. At the end of the loop, you have all of your IDs assigned and an empty List.

Hope this helps, if you need more help let me know.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do Generate Random Unique int ? 1 Answer

Pick between two floats 2 Answers

instantiating vertically 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