• 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
Question by GhostDre · Jul 09, 2014 at 07:07 AM · c#arraysrandom.rangegeneratenumbers

Random Number Generated within Array

Hi guys I want to generate a random number (0-9), for every item I pick up. One random generated number for one item I pick up. I'm having a problem with it being an array, which I know if I set Random.Range(0,10), it will randomly generate one number for all the items, which I don't want. How can I implement the Random.Range to each item with in the array? Here is the code:

  public class RayCasting : MonoBehaviour
     {
         public float pickupDistance;
         public List<Item> items;
     
         
         #region Unity
         void Start ()
         {
             Screen.lockCursor = true;
         }
         void Update ()
         {
             RaycastHit hit;
             Ray ray = new Ray(transform.position, transform.forward);
             if (Physics.Raycast(ray, out hit, pickupDistance))
             {
                 foreach(Item item in items)
                 {
     
                     if(Input.GetMouseButtonDown(0)) {
                         if (item.gameObject.Equals(hit.collider.gameObject))
                     {
                         numItemsCollected++;
                         item.Collect();
                         break;
                             }
                     }
                 }
             }
         }
         
         void OnGUI()
         {
     
             GUILayout.BeginArea(new Rect(130,400,100,100));
             {
                 GUILayout.BeginVertical();
                 {
             if (numItemsCollected < items.Count)
             {
                 foreach (Item item in items)
     
                             GUILayout.Label(string.Format("[{0}] {1}", item.Collected ? "X" : " ", item.name));
             }
             else
             {
                 GUILayout.Label("You Win!");
             }
                 }
                 GUILayout.EndVertical();
         }
             GUILayout.EndArea();
         }
         #endregion
         
         #region Private
         private int numItemsCollected;
         #endregion
     }
     
     [System.Serializable]
     public class Item
     {
         public string name;
         public GameObject gameObject;
         
         public bool Collected { get; private set; }
         
         public void Collect()
         {
             Collected = true;
             gameObject.SetActive(false);
         }
     }
 
Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image

Answer by darthtelle · Jul 09, 2014 at 07:38 AM

 int[] randomArray = new int[10];
 for(int arrayIndex = 0; arrayIndex < randomArray.Length; arrayIndex++)
 {
    randomArray[arrayIndex] = Random.Range(0, 10);
 }

Do you mean like this? Or does the number get generated inside the single Item class??

 int randomNumber;    
 void Awake()
 {
    randomNumber = Random.Range(0, 10);
 }
Comment
GamesOfArcadia

People who like this

1 Show 0 · 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

Answer by kannan21 · Jul 09, 2014 at 07:59 AM

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Collections.Generic;
 
 public class Player : MonoBehaviour 
 {
     public List<Item> items;
     void Start()
     {
         foreach(Item item in items)
             item.SetRandomNumber();
     }
 }
 
 [Serializable]
 public class Item
 {
     public int randomNumber;
 
     public void SetRandomNumber()
     {
         randomNumber = Random.Range (0,9);
     }
 }


Comment
GamesOfArcadia

People who like this

1 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to get random 10 numbers from an integer array 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Randomly Select String from Array 0 Answers

Complicated Question about Arrays and Duplicate Sorting Orders! 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