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 MAVW · Apr 12, 2014 at 03:24 AM · arrayrandomshuffle

Problems with array "shuffle bag"

Hi guys, so.. I'm trying to create an array (dialogArray) to store random values (randomDialog) BUT only once for each value, that way the random value never repeat itself. What should come out of it is a value to be used on another part of the script to choose a dialogue (actualDialog).

But it seems that the random values are never stored (dialogArray.Add(randomDialog);) and so the code keeps running the same numbers as if they were never picked before.

Thanks!

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class init : MonoBehaviour {
 
     private int randomDialog;
     public List<int> dialogArray;
     private int actualDialog = 0;
 
     void Awake()
     {
         Dialoguer.Initialize();
     }
 
     // Use this for initialization
     void Start () 
     {
 
     }
     void getRandomDialog()
     {
         randomDialog = Random.Range(0,11);
     }
 
     // Update is called once per frame
     void Update ()
     {
         dialogArray.Add(40);
         if(Input.GetKeyDown("space"))
         {
             getRandomDialog();
             Debug.Log(randomDialog);
             for( int i=1; i < 8; i++)
             {
                 if(randomDialog == dialogArray[i])
                 {
                     Debug.Log("equal");
                     return;
                 }
                 else if(randomDialog != dialogArray[i])
                 {
                     break;
                 }
             }
             dialogArray.Add(randomDialog);
 
             Debug.Log("Pass");
             actualDialog = randomDialog;
             Debug.Log(actualDialog);
         }
 
     }
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
0

Answer by jackdracon · Apr 12, 2014 at 06:18 PM

So,try this one:

 void Randomize_Cannons()
     {
         var current_Cannons = Cannons;
         List<GameObject> randominzed_Cannons = new List<GameObject>();
         while(current_Cannons.Count > 0)
         {
             var _ind = Random.Range(0, current_Cannons.Count);
             randominzed_Cannons.Add(current_Cannons[_ind]);
             current_Cannons.RemoveAt(_ind);
         }
         Cannons = randominzed_Cannons;
     }

Why you don't try to reset your logic after some time? Or some 'event', 'trigger' inside the game?

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

22 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

Related Questions

Randomly Choosing two objects from an array and switching their positions 3 Answers

RandFuncs.Shuffle question 0 Answers

How to instantiate the first 8 gameobjects in an array (UnityScript)? 1 Answer

how to check for GameObject is null in array with random 1 Answer

How to know what random number is chosen 2 Answers


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