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 BrimTheOne · Aug 19, 2013 at 11:12 AM · arraypositionvector3instatiate

store instantiated in Array. c#

so, i am trying to add instatiated gameobjects position x,y,z to an array, so i can get a random from the list to generate a "Ore" or well, it doesent really matter what, just so i can get a random from the list and get that objects position. But it is really bugging me, this is how far ive comen yet

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TerrainPerlinNoise : MonoBehaviour {
     int chunkheightmapWidth = 100;
     int chunkheightmapHeight = 100;
     public float EpicGenerator;
     private Quaternion rot;
     
     public GameObject cubePrefab;
 
     public GameObject Cubes = null;    
       public Vector3[,,] CubeCount = new Vector3[100,20,100];
     public int _Xpos;
     public int _Ypos;
     public int _Zpos;
     
     OreGen oregen;
     
     int posI = 0;
     int posK = 0;
     
     
     public float Tiling = 10.0f; 
     void Start()
     {
 
         EpicGenerator = Random.Range(0.8f, 1.9f);
         rot.eulerAngles = new Vector3(0,0,0);
         GenerateHeights(Tiling);
 
     }
  
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.I)){
         Debug.Log(Random.Range(0,(CubeCount.Length - 1)));
         
         oregen.generate(CubeCount[3,3,3 ]);    
         }
     }
     
     
     public void GenerateHeights(float tileSize)
     {
         float[,] heights = new float[chunkheightmapWidth, chunkheightmapHeight];
 
         
         for (int i = posI; i < chunkheightmapWidth; i++)
         {
             
             for (int k = posK; k < chunkheightmapHeight; k++)
             {
                 heights[i, k] = Mathf.PerlinNoise(((float)i / (float)chunkheightmapWidth) * tileSize, ((float)k / (float)chunkheightmapHeight) * tileSize)/EpicGenerator;
                  Cubes = Instantiate(cubePrefab, new Vector3(i, heights[i, k], k), Quaternion.identity) as GameObject;
                 Cubes.name ="Cube place: " + i + heights[i, k] + k;
                 
                 CubeCount[_Xpos, _Ypos, _Zpos] = (i + heights[i, k] + k);
 
             }
         }
         
     }
 }

and i literraly have no idea wtf is going on, i give them new names, and i add them to cubecount, but when i ask to debug log CubeCount, it gives me ONE Integer, with 6 digits in it.

Comment
Add comment · Show 6
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 Joyrider · Aug 19, 2013 at 12:20 PM 0
Share

Where do you assign _Xpos _Ypos and _Zpos? It seems like your are always filling the same slot of the cubeCount Array.

avatar image BrimTheOne · Aug 19, 2013 at 12:30 PM 0
Share

well, im still learning about arrays, so i have looked at others people examples and tried to see what i could understand and do with it myself, and this.. this uhm, i dont know about the _Xpos _Ypos _Zpos :S

avatar image Owen-Reynolds · Aug 19, 2013 at 01:13 PM 0
Share

You might try some warm-ups first. Like declare a simple line of cubes: Transform[] CubeLine;. Instantiate using one loop, then use another loop to change all colors on a keypress. Then try with a 2D array Transform[,] CubeGrid; to make a floor.

Big positive ending: the body of the loop looks like Oklahoma.

avatar image ArkaneX · Aug 19, 2013 at 02:09 PM 0
Share

The more I look at the question and code, the more I am confused... For example, what you mean by 'get that objects position'? Position in a game world?

Could you please write once again, what exactly you're trying to achieve?

avatar image BrimTheOne · Aug 19, 2013 at 04:31 PM 0
Share

i got confused too, so i deleted most and tried over again, this time with a more satisfying result, altough it does not work yet. this -

using UnityEngine; using System.Collections; using System.Collections.Generic; public class TerrainPerlinNoise1 : $$anonymous$$onoBehaviour { int chunkheightmapWidth = 32; int chunkheightmapHeight = 32; public float EpicGenerator; private Quaternion rot; public GameObject cubePrefab; public GameObject[] InstanceList; public OreGen oregen; int posI = 0; int pos$$anonymous$$ = 0; public float Tiling = 10.0f; void Start() { EpicGenerator = Random.Range(0.8f, 1.9f); rot.eulerAngles = new Vector3(0,0,0); GenerateHeights(Tiling); } void Update() { if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.I)){ var randomly = Random.Range(0, InstanceList.Length + 1); print(InstanceList[randomly].transform.position); oregen.generate(InstanceList[randomly].transform.position); } } public void GenerateHeights(float tileSize) { float[,] heights = new float[chunkheightmapWidth, chunkheightmapHeight]; var listing = chunkheightmapWidth + chunkheightmapHeight; InstanceList = new GameObject[listing]; for (int i = posI; i < chunkheightmapWidth; i++) { for (int k = pos$$anonymous$$; k < chunkheightmapHeight; k++) { heights[i, k] = $$anonymous$$athf.PerlinNoise(((float)i / (float)chunkheightmapWidth) * tileSize, ((float)k / (float)chunkheightmapHeight) * tileSize)/EpicGenerator; GameObject item = Instantiate(cubePrefab, new Vector3(i, heights[i, k], k), Quaternion.identity) as GameObject; listing = (int)i + (int)heights[i, k] + (int)k; item.transform.parent = transform; InstanceList[listing] = item; } } } }

I want to make it to find a random of the cubeprefabs, and generate another prefab there, i got the thing with the generate there , and it does find a random one, except, it only finds the cubes in direct z, or direct x(see attached pic).. no middle or anything. : alt text

alt text

(if pic wont work - http://imgur.com/k5gdLV6)

Show more comments

1 Reply

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

Answer by ArkaneX · Aug 19, 2013 at 01:21 PM

Length property returns number of elements in the array. If you create one-dimensional array, e.g. someArray[10], then length of dimension 0 (zero) is 10, and it is equal to Length of the whole array. If you create two-dimensional array, e.g. someArray[10,5], then length of dimension 0 is 10, length of dimension 1 is 5, and Length will return 50. You can get the length of given dimension by using GetLength(dimension) method.

In your case, you have three dimensional array, so Length is returning 100*20*100, so your random ranges from 0 to 199999. You have to do:

 var randomX = Random.Range(0, CubeCount.GetLength(0) - 1);
 var randomY = Random.Range(0, CubeCount.GetLength(1) - 1);
 var randomZ = Random.Range(0, CubeCount.GetLength(2) - 1);
 
 var randomElement = CubeCount[randomX, randomY, randomZ];

EDIT:

Ok - after all the comments, I think I now get the gist of your problem, although I have to admit it would be hard without image :)

You want to have a terrain 32x32 tiles with random heights, and then you want to generate ore (or anything else) on top of random tile. Correct me if I'm wrong :)

If the above is ok, then I'd do it that way:

 using UnityEngine;
 
 public class TileGen : MonoBehaviour {
     
     public GameObject cubePrefab;
     int chunkheightmapWidth = 32;
     int chunkheightmapHeight = 32;
     public float EpicGenerator;
     public float Tiling = 10.0f; 
     
     // two dimensional array of items - storing in one dimensional is also possible, but not the way you did
     public GameObject[,] items;
     // small addition, to check if given coordinates was already used
     private bool[,] alreadyGenerated;
     
     void Start()
     {
         EpicGenerator = Random.Range(0.8f, 1.9f);
         GenerateItems(Tiling);
     }
      
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.I))
         {
             var randomX = Random.Range(0, items.GetLength(0) - 1);
             var randomZ = Random.Range(0, items.GetLength(1) - 1);
             print("x: " + randomX + ", z: " + randomZ);
             GenerateOre(randomX, randomZ);
         }
     }
     
     private void GenerateOre(int x, int z)
     {
         if(alreadyGenerated[x, z])
         {
             Debug.Log ("already generated at this location");
             return;
         }
         
         // get item on top of which you want to create ore
         var item = items[x,z];
         var ore = GameObject.CreatePrimitive(PrimitiveType.Sphere);
         ore.transform.position = item.transform.position + new Vector3(0, item.transform.localScale.y, 0);
         alreadyGenerated[x, z] = true;
     }
     
     public void GenerateItems(float tileSize)
     {
         items = new GameObject[chunkheightmapWidth, chunkheightmapHeight];
         alreadyGenerated = new bool[chunkheightmapWidth, chunkheightmapHeight];
         
         for (int x = 0; x < chunkheightmapWidth; x++)
         {
             for (int z = 0; z < chunkheightmapHeight; z++)
             {
                 var height = Mathf.PerlinNoise(((float)x / (float)chunkheightmapWidth) * tileSize, ((float)z / (float)chunkheightmapHeight) * tileSize)/EpicGenerator;
                 var item = (GameObject)Instantiate(cubePrefab, new Vector3(x, height, z), Quaternion.identity);
                 item.transform.parent = transform;
                 items[x,z] = item;
                }
         }
     }
 }

If it's ok, but you don't have a clue why some part of it works, please let me know, as I want you to understand this.

Comment
Add comment · Show 15 · 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 BrimTheOne · Aug 19, 2013 at 01:33 PM 0
Share

i did like this if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.I)){ var randomX = Random.Range(0, CubeCount.GetLength(0) - 1); var randomY = Random.Range(0, CubeCount.GetLength(1) - 1); var randomZ = Random.Range(0, CubeCount.GetLength(2) - 1);

         Debug.Log(CubeCount[randomX, randomY, randomZ]);
         }

but this just prints "0" ?

avatar image ArkaneX · Aug 19, 2013 at 02:01 PM 0
Share

It's a bit strange, as in your code CubeCount is an array of Vector3. Vector3 is struct with default value (0.0, 0.0, 0.0). And if you haven't updated elements after initialization, you should get that default value in the console.

Have you probably changed CubeCount array type to int[,,] or long[,,]?

avatar image BrimTheOne · Aug 19, 2013 at 02:07 PM 0
Share

i did actually change it into float, because it gave me a error with the vector3 at another line.. Would it be better to make it vector3 and just fix the $$anonymous$$or error?

avatar image ArkaneX · Aug 19, 2013 at 02:32 PM 0
Share

It all depends on what you really want to achieve. In this answer I gave you an info, about how to get a random element from multidimensional array. But reading a few more times through your question, I got confused. Please see my comment under question, and provide a bit more of explanation.

avatar image BrimTheOne · Aug 19, 2013 at 05:22 PM 0
Share

I have tried to explain it a little better in the other question :-)

Show more comments

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

19 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

Related Questions

NGUI - Vector 3 Position different from position 1 Answer

How can i get the index of an array[] as int? 0 Answers

Moving an Object to the vector of other objects on button press using a vector3 array? 0 Answers

Array.Push() for Vector3[] or how to add items to Vector3 array without knowing index 1 Answer

Problem with Prefab 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