Hi all!
please help this newbie with (I hope solvable) problem:
I have an island (imported from Maya) and want to multi-plicate a gameobject randomly over the island. gameobjects must be above the surface and I must have a control of how many of them and how high above the surface they’ll be multiplicated (to a height span).
Is it possible?
if yes, I would be extremely grateful for a code.
tnx in advance and best regards!
b
Yes, it is very possible 
This is the guide I used to get things spawning in my game :
http://unity3d.com/support/documentation/Manual/Instantiating%20Prefabs.html
That will give you the basics
Good Luck!
thanks for this, but it is only about multiplying objects. now I need to master the hard part - to multiply them just above the mesh of an “island”. please help.
thanks in advance!
Instantiate takes a position point. Or you can alter their transform.position. Just create a point that takes your island’s transform.position and adds some value to its Z component.
tnx, but how do you create a point that takes “island’s” (gameobject’s) transform.position?
var point : Vector3 = new Vector3(myIslandGameObject.transform.position.x, myIslandGameObject.transform.position.y, myIslandGameObject.transform.position.z + 10);
it keeps on saying “Unknown identifier error” for my Island gameobject (I tried even with simple capsule instead and it’s the same error. where is the trick?
tnx!
How do you reference/assign your island gameobject in your script?
I’m not sure if I understand your question. 
do you mean how does my code look? :
var point : Vector3 = new Vector3(otokamayapolygons.transform.position.x, otokamayapolygons.transform.position.y, otokamayapolygons.transform.position.z + 10);
otokamayapolygons is a name of my gameobject (imported from maya).
tnx angain!
Are you looking for something like this? (I didnt verify it, it is probably close though)
var offset : float = 50;
function Start(){
var otok : GameObject=GameObject.Find("otokamayapolygons");
var bounds : Bounds=otok.GetComponent(MeshFilter).mesh.bounds;
var point : Vector3 = new Vector3(
Random.Range(bounds.min.x, bounds.max.x),
Random.Range(bounds.min.y, bounds.max.y) + offset,
Random.Range(bounds.min.z, bounds.max.z));
}
did you mean like this:
var offset : float = 50;
function Start(){
var otok : GameObject=GameObject.Find(“otoka_maya_polygons”);
var bounds : Bounds=otok.GetComponent(MeshFilter).mesh.bounds;
var point : Vector3 = new Vector3(
Random.Range(bounds.min.x, bounds.max.x),
Random.Range(bounds.min.y, bounds.max.y) + offset,
Random.Range(bounds.min.z, bounds.max.z));
Instantiate(prefab, point, Quaternion.identity);
}
this doesn’t work. no error, but no result. know why?
tnx!
sorry, first line missing.
this is it:
var prefab : GameObject;
var offset : float = 50;
function Start(){
var otok : GameObject=GameObject.Find(“otoka_maya_polygons”);
var bounds : Bounds=otok.GetComponent(MeshFilter).mesh.bounds;
var point : Vector3 = new Vector3(
Random.Range(bounds.min.x, bounds.max.x),
Random.Range(bounds.min.y, bounds.max.y) + offset,
Random.Range(bounds.min.z, bounds.max.z));
Instantiate(prefab, point, Quaternion.identity);
}
still doesn’t work.
Try this:
var prefab : GameObject;
var offset : float = 50;
function Start()
{
var otok : GameObject=GameObject.Find("otoka_maya_polygons");
var point : Vector3 = new Vector3
(
otok.transform.position.x,
otok.transform.position.y + offset,
otok.transform.position.z
);
Instantiate(prefab, point, Quaternion.identity);
}
tnx fizixMan!
as you can see above, I tried, but no effect.
this is better:
var prefab : GameObject;
var offset : float = 50;
function Update(){
var otok : GameObject=GameObject.Find(“nurbsToPoly1”);
var bounds : Bounds=otok.GetComponent(MeshFilter).mesh.bounds;
var point : Vector3 = new Vector3(
Random.Range(bounds.min.x, bounds.max.x),
Random.Range(bounds.min.y, bounds.max.y) + offset,
Random.Range(bounds.min.z, bounds.max.z));
Instantiate(prefab, point, Quaternion.identity);
}
where “nurbsToPoly1” is a mesh of my gameobject. now, if I use function start it produces only one gameobject, but if I do as above (Update) it doesn’t stop. how can I limit cycle No.?
thanks again to all of you! really great guys/girls.
this is not working like it should. flies are created inside the island as well, but they should only be in the air just above the surface.
I think the position on the x-z plane must be random (inside island bounds) and then y (height in my case) calculated from the mesh height at this exact point. but that’s way too much for me.
code:
var prefab : GameObject;
var offset : float = 2;
var flies = 20;
function Start(){
for (var i : int = 0;i < flies; i++){
var otok : GameObject=GameObject.Find(“nurbsToPoly1”);
var bounds : Bounds=otok.GetComponent(MeshFilter).mesh.bounds;
var point : Vector3 = new Vector3(
Random.Range(bounds.min.x, bounds.max.x),
Random.Range(bounds.min.y, bounds.max.y)+ offset ,
Random.Range(bounds.min.z, bounds.max.z));
Instantiate(prefab, point, Quaternion.identity);
}
}
Change:
Random.Range(bounds.min.y, bounds.max.y)+ offset ,
to:
bounds.max.y+ offset ,
still isn’t ok. all GO are at the same height, just above the highest point of the “island”. 
var prefab : GameObject;
var offset : float = 2;
var flies = 20;
function Start(){
for (var i : int = 0;i < flies; i++){
var otok : GameObject=GameObject.Find(“nurbsToPoly1”);
var bounds : Bounds=otok.GetComponent(MeshFilter).mesh.bounds;
var point : Vector3 = new Vector3(
Random.Range(bounds.min.x, bounds.max.x),
bounds.max.y+ offset ,
Random.Range(bounds.min.z, bounds.max.z));
Instantiate(prefab, point, Quaternion.identity);
}
}
If your island has a MeshCollider attached to it, then you could issue a Physics.Raycast down from that point. Where the ray collides with the island you could then shift that hit point up “offset”.
Please Googles the keywords there, check the script reference, and give them a shot before you ask “how do I do that?” Post your trial code and we’ll go from there.
EDIT: the other, perhaps simpler option is instead of always shifting by “offset” you shift by a random number. (if these are “flies”, the insect, this might make more sense than having them coincidentally follow the contours of your island!)
me again 
is this the right way? surely it isn’t the end, since all flies are created in one single x,z point (varying only y). where is a bug (or better a “bug family”)?
var prefab : GameObject;
var offset : float = 200;
var flies = 20;
function FixedUpdate(){
for (var i : int = 0;i < flies; i++){
var otok : GameObject=GameObject.Find("nurbsToPoly1");
var bounds : Bounds=otok.GetComponent(MeshFilter).mesh.bounds;
var point : Vector3 = new Vector3(
Random.Range(bounds.min.x, bounds.max.x),
Random.Range(bounds.min.y, bounds.max.y),
Random.Range(bounds.min.z, bounds.max.z));
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
hit.point.y += offset;
Instantiate(prefab, hit.point, Quaternion.identity);
Debug.DrawLine (transform.position, hit.point);
}
}
}
I think you are close, but you aren’t using your random ‘point’ var. Maybe you want to do this:
Physics.Raycast (point, -Vector3.up, hit)