- Home /
How to call this function only once?
I have a Random.ColorHSV
that randomizes, well, a color. The problem is , I have about 10 objects with this script running the Random.ColorHSV
, so it randomizes 10 times. How can I have it randomized only once?
Thanks in advance!
Answer by Tsaras · Mar 20, 2019 at 10:11 PM
If you mean you want to random the color once and share it within the 10 objects, you can have a super-class do the randoming and store it there for the 10 objects to just access it as a property.
EDIT: You can do it a couple of ways, an easy one is to make a new script and attach it to a gameobject in your scene that could be acting as a game manager (having functions to manage the game's state or so) and make it randomize the color at the start of the game.
public Class GameManager : Monobehaviour {
public static Color randomizedColor;
void Start(){ //you can also use Awake here if the objects take their color in their Start method
randomizedColor = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
}
}
And then in your objects you can call:
currentObsColor = GameManager.randomizedColor;
Excuse my ignorance, but how exactly do I go about this? I tried my best but the result is the same.
Here's my class:
public class RandomizeObstacleColor
{
public Color randomizedColor =
Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
}
And here is my reference:
currentObsColor = new RandomizeColor.RandomizeObstacleColor().randomizedColor;
Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612193954im_/https://answers.unity.com/themes/thub/images/avi.jpg)