Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 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
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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
Add comment · 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
1

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Randomly Select String from Array 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Obtaining the Variable of a Prefab in an array. 1 Answer

How do i convert this code snippet to a list? 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