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 heycci · Jun 09, 2013 at 01:45 PM · gameobjectrandomarray of gameobjectsmultiple objects

Retrieve multiple items from an array of GameObjects

Hello,

I'm quite new to JavaScript. I've managed to create a 100 cube array and disable them one at a time but I would like to be able to retrieve more than one of those cubes (a variable amount) in that array and disable them at the same time.

 var goPrefab : GameObject;
 var xEdge = 5;  // width
 var yEdge = 20;  // height
 var zEdge = 1;  // depth
 var v3Center = Vector3.zero;
 
 
 private var arCube : GameObject[,,] = new GameObject[xEdge, yEdge, zEdge];
  
 function Start() {
     for (var i = 0; i < xEdge; i++) {
        for (var j = 0; j < yEdge; j++) {
          for (var k = 0; k < zEdge; k++) {
           var x = v3Center.x + xEdge - i - 0;
           var y = v3Center.y + yEdge - j - 1;
           var z = v3Center.z + zEdge - k - 1;
           arCube[i,j,k] = Instantiate(goPrefab, Vector3(x,y,z), Quaternion.identity);
          }
        }
     }
 }
 
  
 function Update()
 {
 
 if (Input.GetMouseButtonDown(0)) {
         
         var Cube : GameObject = arCube[Random.Range(0, xEdge), Random.Range(0, yEdge), Random.Range(0, zEdge)];
         
         if (Cube.renderer.enabled) {
         Cube.renderer.enabled = false;
           }     
           else { Update(); }
           
     }
 }


Could it be possible, for example, to repeat the last part an X number of times?

Comment
Add comment · Show 2
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 Fattie · Jun 09, 2013 at 01:46 PM 0
Share

it's not really how you do $$anonymous$$ecraft. there are a huge number of questions on here to help wit this, please search. there is a complete "$$anonymous$$ecraft starter package" that you only have to download and use.

avatar image heycci · Jun 09, 2013 at 01:50 PM 0
Share

I'm not trying to apply this to $$anonymous$$ecraft. It's for a school project on data visualization, so any possible way to make this work would be highly appreciated.

2 Replies

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

Answer by robertbu · Jun 09, 2013 at 02:42 PM

You can add a 'do/while' loop to delete 5 cubes. Note that you will also have to keep track of the total number of cubes so that the code does not hang. In addition Random.Range() may need to be called many times when there are few cubes. I think you will be okay, but there may be a frame rate drop towards the end of the cube hiding. You might be better off creating a list of the cubes, shuffling it, then deleting cubes in order from the list.

 #pragma strict
 
 var goPrefab : GameObject;
 var xEdge = 5;  // width
 var yEdge = 20;  // height
 var zEdge = 1;  // depth
 var v3Center = Vector3.zero;
 private var total : int;
  
 private var arCube : GameObject[,,] = new GameObject[xEdge, yEdge, zEdge];
  
 function Start() {
     total = xEdge * yEdge * zEdge;
     
     for (var i = 0; i < xEdge; i++) {
        for (var j = 0; j < yEdge; j++) {
          for (var k = 0; k < zEdge; k++) {
           var x = v3Center.x + xEdge - i - 0;
           var y = v3Center.y + yEdge - j - 1;
           var z = v3Center.z + zEdge - k - 1;
           arCube[i,j,k] = Instantiate(goPrefab, Vector3(x,y,z), Quaternion.identity);
          }
        }
     }
 }
 
 function Update() {
     if (Input.GetMouseButtonDown(0)) {
          var count = 5;
         do {
             if (total <= 0) return;
             
             var Cube : GameObject = arCube[Random.Range(0, xEdge), Random.Range(0, yEdge), Random.Range(0, zEdge)];
  
                if (Cube.renderer.enabled) {
                    Cube.renderer.enabled = false;
                  count--;
                  total--;
             }
         } while (count > 0);
     }
 }
Comment
Add comment · Show 1 · 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 heycci · Jun 09, 2013 at 03:00 PM 0
Share

Thank you so much for the quick reply, this works perfectly! I was thinking it could be solved with the while loop, but couldn't quite figure out how to get it work.

avatar image
0

Answer by DuckOfDoom · Jun 09, 2013 at 02:47 PM

I have no idea how it is done in javascript but in c# I'd do something like this:

             //Make a new list for cubes to be disabled
             List<GameObject> cubesToDisable = new List<GameObject>();
 
             while (cubesToDisable.Count <= 10)
             {
                 //Select one cube
                 var cube = arCube[Random.Range(0, xEdge), Random.Range(0, yEdge), Random.Range(0, zEdge)];
                 //If its not already in list, add it. Else discard it and dont increment the counter.
                 if (!cubesToDisable.Contains(cube))
                 {
                     cubesToDisable.Add(cube);
                 }
             }
                 //Easier way to do a foreach loop on list
             cubesToDisable.ForEach(cube =>
                 {
                     if (cube.renderer.enabled)
                         cube.renderer.enabled = false;
                 });

This takes 10 cubes from your array and disables them. But beware, if you don't have 10 unique cube objects in array, you'll be stuck in infinite loop.

Comment
Add comment · Show 1 · 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 heycci · Jun 09, 2013 at 03:01 PM 0
Share

Thank you for your input! I'm not familiar with c#, but thankfully robertbu's answer suits my needs perfectly :)

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

16 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

Related Questions

Three Spots For Three Random Objects 1 Answer

Generating a Displacement Map in Unity3d? 1 Answer

Lightning with random intervals 2 Answers

Moving GameObject to various position ? 1 Answer

random limitation memory game 0 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