I’ve been banging my head against this problem for 2 days and I feel like there must be something I’m missing.
I’m trying to instantiate a number of prefabs based on 2 criteria: quantity and maximum distance. So for example I would generate 15 trees spread out in a 200 meter radius. For some reason my code keeps instantiating all the trees on top of one another at 0,0.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class maybecreatetree : MonoBehaviour
{
public GameObject treegen;
public int quantity = 20;
public int maxdist = 500;
void Start()
{
for (int i = 0; i< quantity; i++)
{
Instantiate(treegen, new Vector3 (UnityEngine.Random.Range(-maxdist, maxdist),0, UnityEngine.Random.Range(-maxdist, maxdist)), UnityEngine.Random.rotation);
print("thats a tree right there");
}
}
}
This code is sitting on an empty gameobject which is also generating Perlin noise terrain with a different script.
I’ve tried a few different things although I suspect I’m just misunderstanding how to use Vector3.
Any help would be greatly appreciated.
But one guess could be that you are passing in an int (maxdist) where a float is expected. I’m unsure how unity / c# handles that without trying it myself.
Either way Print() out a vector3 to confirm the values, then assign it with the instantiate
Adding to Mr Tree’s suggestion, I’m going to guess that maxdist is actually zero or something small… since it is public, if you save this scene when it is zero, Unity’s serialization will overwrite your 500 class initialization value. Look in the scene or prefab to be sure.
Try directly typing floats in there (though it sounds like you covered that)
Try assigning transform.position the line directly after instantiation of it.
Try printing its Transform.position directly after instantiation and again later somewhere.
Is it reading at the correct positition when created and assigned, and then later in the same frame set to 0,0 maybe something else is resetting or moving it.
Is the prefab set to freeze position or anything.
Try the same code but instantiating a different prefab, some simple object like a cube or something.
So I just copy and pasted your original code from post#1 exactly as is and it works correct if the prefab is just a basic cube game object. cubes get generated in random positions.
You must have something else going on with the treegen itself or code elsewhere doing something funny.
also look at this
for (int i = 0; i < quantity; i++)
{
GameObject t;
t = Instantiate(treegen, new Vector3(UnityEngine.Random.Range(-maxdist, maxdist), 0, UnityEngine.Random.Range(-maxdist, maxdist)), UnityEngine.Random.rotation);
print(t.transform.position);
}
for me, i see all the random positions.
Do you get that?
If you do, but then the object is at 0,0 and also reads 0.0 later on (later on meaning possibly in the same frame even) then something is resetting it
I fixed it (sort of). By attaching the tree generator script to a cube prefab and then using that as the treegen game object, for whatever reason the game handles it just fine. I wish it would work on just an empty object but for whatever reason it wont. The other oddity is that while it does generate trees all over the distance, for each one it still also generates an identical tree at 0,0. So I could have 50 trees all across the horizon, and then a 50-tree Frankenstein at 0,0.
It’s working enough for my purposes, so I’m going to leave it for now and maybe I’ll write my own tree generator later. Below is the one I used for this experiment. (Probably should have mentioned that at the outset admittedly).
Did you ever try to set the transform.position right after instantiation instead of inside the function? If you instantiate with a new position, and then the tree is immediately setting its own position… The very next line you might be able to just set the position there.
Or push them to a List and change the positions “later”, buying you time to position after the trees custom placement script.
Or of course you can look under the hood and see what the tree is doing too.
Or having them in an empty game object like you have is probably just fine too.