- Home /
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.
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.
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
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.
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 ;)
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613180239im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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