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 /
  • Help Room /
avatar image
2
Question by alb917 · Oct 29, 2012 at 05:39 PM · c#arrayspoolrecycle

Array of Arrays

Was wondering if anyone could help, I'm trying to create a Temple Run style iOS game, so I'm recycling the platforms at the moment rather than using Instantiate and Destroy. At the moment, the platforms are created at Start, and are random, but then are recycled in the same order after the first 20. I want to create an Array of Arrays, so have 4 different groups of say 20 different models of platforms, which a group is chosen using randomNumber (Element 0-3), and then use randomNumber again to chose out of which of those 20 platforms to select randomly from the chosen group. This is my code at the moment:

 using UnityEngine;
 using System.Collections.Generic;
 
 public class PlatformManager : MonoBehaviour {
     
     public Transform[] prefabs;
     public Transform[] platform1;
     public Transform[] platform2;
     public Transform[] platform3;
     public Transform[] platform4;
     
     
     
     public int numberOfObjects;
     public float recycleOffset;
     public Vector3 minSize, maxSize, minGap, maxGap;
     public float minY, maxY;
 
     
     
     private GameObject[] TileGameObjects;
     private Vector3 nextPosition;
     private Queue<Transform> objectQueue;
 
     void Start () {
         
         objectQueue = new Queue<Transform>(numberOfObjects);
         for(int i = 0; i < numberOfObjects; i++){
             int randomNumber = Mathf.Abs (Random.Range(0, prefabs.Length));
             objectQueue.Enqueue((Transform)Instantiate(prefabs[randomNumber]));
         }
         
             
         nextPosition = transform.localPosition;
         for(int i = 0; i < numberOfObjects; i++){
             //int randomNumber = Mathf.Abs (Random.Range(0, prefabs.Length));
             //objectQueue.Enqueue((Transform)Instantiate(prefabs[randomNumber]));
             Recycle();
         }
     }
 
     void Update () {
         if(objectQueue.Peek().localPosition.x + recycleOffset < Runner.distanceTraveled){
             Recycle();
         }
     }
 
     private void Recycle () {
         Vector3 scale = new Vector3(
             Random.Range(minSize.x, maxSize.x),
             Random.Range(minSize.y, maxSize.y),
             Random.Range(minSize.z, maxSize.z));
             
 
         Vector3 position = nextPosition;
         position.x += scale.x * 0.5f;
         position.y += scale.y * 0.5f;
         
         Transform o = objectQueue.Dequeue();
         o.localScale = scale;
         o.localPosition = position;
         
         
         objectQueue.Enqueue(o);
         Debug.Log("alert!");
         
         nextPosition += new Vector3(
             
             
             Random.Range(minGap.x, maxGap.x) + scale.x,
             Random.Range(minGap.y, maxGap.y),
             Random.Range(minGap.z, maxGap.z));
     
         
         if(nextPosition.y < minY){
             nextPosition.y = minY + maxGap.y;
         }
         else if(nextPosition.y > maxY){
             nextPosition.y = maxY - maxGap.y;
         }
     }
 }

I have only created the variables for Platform1, 2, 3 and 4, but not sure how to integrate them inside the prefabs Array.

If anyone could help I would really appreciate it!

Comment
Add comment · Show 3
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 Eric5h5 · Oct 29, 2012 at 06:01 PM 0
Share

That's unreadable; please format your code properly.

avatar image alb917 · Oct 29, 2012 at 06:09 PM 0
Share

@Eric5h5 Sorry, first time posting on here! :)

avatar image fafase · Oct 29, 2012 at 06:22 PM 0
Share

2D arrays could do it.

3 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by sparkzbarca · Oct 29, 2012 at 06:49 PM

yea you need an array where each item in the array is an array itself

for example

 //creates an array of arrays
 int[][] myarray;
 
 //creates an array which contains 20 arrays the arrays are all empty and unitialized, they will need to be initialized, there is currently just 20 starting points reserved in memory. They point to nothing really.
 myarrays = new int[20][];
 
 //the first array is size 3 with the element 0 & 1 & 1
 myarrays[0] = {0,1,1};
 
 myarrays[0][0] = ? //based on the statement above it equals 0
 myarrays[0][1] = ? //based on the statement above it equals 1
 myarrays[0][2] = ? //based on the statement above it equals 1 as well
 
 
 //the following code will take every array in myarrays and make them equal to arrays of size 10, the arrays are empty but now they point to something, its probably zero by default due to internal code.
 for (int x = 0; x <= myarrays.size(); x++)
 {
     myarrays[x] = new int[10];
 }
 //technically the code above still hasnt set what the value of each element in the arrays are, it simply now says its size is definitely 10, internally the code has probably set those memory values to zero however to help prevent unexpected errors, that's not guaranteed I don't think however and it would probably be best to set each value to be certain different machines and things dont do things differently and create unexpected and hard to find errors.
 
 
 //the follow code will create an array that contains 20 arrays, each of those arrays are of size 10, and each of them will have the value set to 0;
 
 int[][] myarrays = new int[20][];
 
 //careful on this for statement, it is less than not less than or equal too, this has to do with size starting to count from 1 and x starting to count from 0. You must start from 0 because the first index in an array is 0.
 
 for(int x = 0; x < myarrays.size(); x++)
 { 
      myarrays[x] = new int[10];
    for (int y = 0; y < myarrays[x].size(); y++)
     {
        myarrays[x][y] = 0;
     }
 }
 
 //Do note that i used the size function to get the size, thats because you shouldn't used numbers like 20 or 10 even if thats what you think you want. You should declare a constant called NUMBER_OF_ARRAYS and NUMBER_OF_ELEMENTS_IN_ARRAYS or something like that and substitute those that way you can change the size of the arrays with a few keyboard strokes.


mark as answered and have a nice day!

Comment
Add comment · Show 7 · 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 fafase · Oct 30, 2012 at 06:45 AM 0
Share

Hmmm, but what about making simple like:

 int array[,] = new int[2,2]; 
 array[0,0] = 1;
 array[0,1] = 2;
 array[1,0] = 3;
 array[1,1] = 4;
 

and nothing else.

avatar image sparkzbarca · Oct 30, 2012 at 07:06 AM 0
Share

you certainly could. You'll notice if you want to change anything you'll have to go and manually change values perhaps many values over and over.

I used my array so you could change stuff. If your dead on certain your not going to ever need to change the array during runtime or the size or anything later go ahead and use literal values.

I went into detail explaining how to do stuff because clearly the man has never used multidimensional arrays before and seeing the many ways they can be created and modified is appropriate. If you understand what they are already you'll simply create and use them as you did there. :P

avatar image fafase · Oct 30, 2012 at 07:11 AM 0
Share

Ok now I understand your point. But I'm going to sound a little like a nagger but I would say then, if you do not know what the size going to be at run-time, use a list ins$$anonymous$$d. Well, he got what he needs now. Since you tried and did explain, you get a +1 :)

avatar image sparkzbarca · Oct 30, 2012 at 07:22 AM 0
Share

yea whoops i didnt mean runtime change (impossible of course with static arrays) but simply deciding later on you want the array larger even if its size is still static.

avatar image alb917 · Nov 05, 2012 at 08:14 PM 0
Share

@sparkzbarca thanks for your help! I've been trying for a while now to integrate your answer into my original code and I'm not sure exactly where to put it, as wherever I try to put the code, the editor doesn't seem to like it?

Show more comments
avatar image
1

Answer by fafase · Oct 30, 2012 at 06:52 AM

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class WaitC : MonoBehaviour {
     const int row = 2;// My values are smaller than 4 and 20...
     const int col = 3;
     Transform [,]array = new Transform[row,col];
     void Start(){
         GameObject[] obj=GameObject.FindGameObjectsWithTag("Platform");
         array[0,0]= obj[0].GetComponent<Transform>();
         array[0,1] =obj[1].GetComponent<Transform>();
         array[0,2] =obj[2].GetComponent<Transform>();
         array[1,0] =obj[3].GetComponent<Transform>();
         array[1,1] =obj[4].GetComponent<Transform>();
         array[1,2] =obj[5].GetComponent<Transform>();
         for(int i = 0;i<2;i++){
             for(int j = 0; j<3;j++)print (array[i,j].transform.position);
         }
     }    
 }

This is an example with 2Darray of Transform. Now for choosing random parts, if I understood right you want to choose a random index and then a random part.

 int a = Random.Range(0,row); int b = Random.Range(0,col);
 Transform newPlatform =  array[a,b];
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
0

Answer by Wasan_Sari · Feb 22, 2016 at 03:01 PM

Hello I tried to to initialize my array of arrays using ( myarrays[0] = {0,1,1};) in c# but it keep giving me errors on the symbol { .

However I manage to do that by using : jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 }; jaggedArray[1] = new int[] { 0, 2, 4, 6 }; jaggedArray[2] = new int[] { 11, 22 };

see this website: https://msdn.microsoft.com/en-us/library/2s05feca.aspx

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

14 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

Related Questions

Null reference exception in an if statement that checks for it. 1 Answer

I have an array from which spawns a random image. Instantiate spawning full pool on start 1 Answer

How to save a two-dimensional array, as part of the variable inspector? 0 Answers

Increase List of Spawns Dynamically 1 Answer

A box is going to follow a simple path (Doesn't work). 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