- Home /
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.
Where do you assign _Xpos _Ypos and _Zpos? It seems like your are always filling the same slot of the cubeCount Array.
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
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.
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?
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. :
(if pic wont work - http://imgur.com/k5gdLV6)
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.
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" ?
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[,,]?
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?
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.
I have tried to explain it a little better in the other question :-)
Your answer
Follow this Question
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