Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 GamingOink · Mar 12, 2017 at 03:03 PM · randomsystemintdebug.log

Random number not printing in console?

I am very new to Unity and have little experience in C#. In my code I am trying to generate a random number, then have the Console print it. I have googled this problem a lot but still cannot find a solution. Any suggestions? (Everything is enabled in the console).

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine; 


 public class LaserShooter : MonoBehaviour {

     public static System.Random rand = new System.Random();
     public int modChoose = rand.Next(1,4);

     void Start () {
     }
 

     void Update () {
         Debug.Log (modChoose);
     }
             
 } 
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 Commoble · Mar 12, 2017 at 04:57 PM

First, an explanation:

In Unity, when you make a class that derives from MonoBehaviour and add it to a GameObject as a script component, each of your public variables become visible in the Inspector for that GameObject. When the game starts and the scene loads up and the object initializes and all the scripts on that object initialize, unity takes all the values that were set in the Inspector and sets all your public variables' values to those values. This means that when you set your public variable's values when you declare them:

public int modChoose = rand.Next(1,4);

then this random value will be overwritten by the Inspector values.

Whenever you need to initialize a variable based on some function, you should set it in the Awake or Start event:

 void Awake()
 {
     this.modChoose = rand.Next(1,4);
 }

Additionally, whenever you have a variable that you don't want to set in the inspector, you should prevent it from appearing in the inspector in the first place to avoid confusion. You can do this by setting the variable to private, or using [System.NonSerialized]:

 private int modChoose;
 // or:
 [System.NonSerialized] public int modChoose;
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 GamingOink · Mar 13, 2017 at 03:15 AM 0
Share

Ok I shall try that

avatar image SnStarr · Mar 13, 2017 at 03:27 AM 0
Share
 [HideInInspector] public int modChoose;

also works..Good thing about Unity. there is a hundred different ways to skin a cat

avatar image GamingOink · Mar 13, 2017 at 03:42 AM 0
Share

Never $$anonymous$$d, I used your solution and it was still not working. The reason was I didn't assign the script to a Game Object. Thank you for responding though!

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

Need help with random spawning system. 1 Answer

pick a random int with the value of 1 from an array 2 Answers

Accessing local system ( File Browser ) 2 Answers

Cross Platform(including WSA)Getting different random number series(in WSA) even after using same System.Random seed for all platforms 1 Answer

Lottery system 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