- Home /
How to set a sprite's location to another sprite based on RNG.
Hi!
I'm having trouble setting up a script (C#) that, generates a random number from 1-5, then, based on that number moves the position of an object to another.
Basically i'm making a 'mining' type game where there is a hidden gem in one of 5 rocks on the screen, the objective is to get the gem within a time limit. I think this is how you would set up the random number generator :
float loc = Random.RandomRange(1, 5);
'loc' being the variable it's stored in.
but past that i'm not sure on where to go :/
i think it would be something like:
if(loc <= 1){
gem.transform.position = transform.TransformPoint (0, 0, 0);
}
etc. but yeah, im not sure xD
Thanks
Answer by hexagonius · Nov 25, 2016 at 12:53 AM
I would create a public gameobject array and place several gameobjects in the scene (where the gem could be) and add those in the inspector to the array. then I would go:
gem.transform.position = positionsArray[RandomRange(0, positionsArray.Length)].transform.position;
Your answer
Follow this Question
Related Questions
moving random objects to random positions 1 Answer
Random Y Axis Direction On Prefab Within Boundaries (C#) 0 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
C# Random spawn from an array 0 Answers