Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 pigaroos · Mar 20, 2019 at 10:06 PM · instancesrandomize

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!

Comment
Add comment
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

1 Reply

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

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;
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 pigaroos · Mar 20, 2019 at 11:48 PM 0
Share

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!

avatar image Tsaras pigaroos · Mar 21, 2019 at 06:24 AM 1
Share

I edited my answer above.

avatar image pigaroos Tsaras · Mar 22, 2019 at 06:02 PM 0
Share

Worked great, thanks!

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

101 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 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 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 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

How do I generate this type of terrain randomly on runtime? 1 Answer

Randomly remove list values 1 Answer

Comparing PhysicMaterials at Runtime 3 Answers

How to have unique instances of script on multiple instances of prefab? 1 Answer

Problem accessing public instances 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