I’m getting now the message “too far out”
This is the WallsTest script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallsTest : MonoBehaviour
{
public GameObject prefab;
public Vector3 wallsStartPosition;
public float width = 0;
public float height = 1;
public float lengthX = 2;
public float lengthZ = 2;
public float time = 1;
public Camera wallsCamera;
private List<GameObject> walls = new List<GameObject>();
private IEnumerator coroutine;
void Start()
{
wallsCamera.transform.position = new Vector3(wallsStartPosition.x, wallsStartPosition.y + 100, wallsStartPosition.z - 250);
for (int i = -2; i < 2; i++)
{
GameObject go = Instantiate(prefab);
go.transform.parent = transform;
Vector3 scale = Vector3.one;
Vector3 adjustedPosition = wallsStartPosition;
float sign = Mathf.Sign(i);
if ((i * sign) % 2 == 0)
{
adjustedPosition.x += (lengthX * sign) / 2;
scale.x = width;
scale.y = height;
scale.z *= lengthZ + width;
}
else
{
adjustedPosition.z += (lengthZ * sign) / 2;
scale.x *= lengthX + width;
scale.y = height;
scale.z = width;
}
adjustedPosition.y -= height / 2;
go.transform.localScale = scale;
go.transform.localPosition = adjustedPosition;
walls.Add(go);
}
coroutine = raiseWall(walls[0], height, time, 0, 100);
StartCoroutine(coroutine);
coroutine = raiseWall(walls[2], height, time, 0, 100);
StartCoroutine(coroutine);
coroutine = raiseWall(walls[1], height, time, 0, 100);
StartCoroutine(coroutine);
coroutine = raiseWall(walls[3], height, time, 0, 100);
StartCoroutine(coroutine);
}
IEnumerator raiseWall(GameObject wall, float amount, float time, float delay, float steps)
{
Vector3 position = wall.transform.localPosition;
float finalHeight = position.y + amount;
yield return new WaitForSeconds(delay);
while (position.y < finalHeight)
{
float remainingHeight = finalHeight - position.y;
position.y += Mathf.Min(remainingHeight, amount / steps);
wall.transform.localPosition = position;
yield return new WaitForSeconds(time / steps);
}
yield break;
}
}
In your code i’m getting the values i checked with break point from the WallsTest:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnObjects : MonoBehaviour
{
public int numberOfObjects;
private int currentObjects;
public GameObject objectToPlace;
private int wallsLengthX;
private int wallsLengthZ;
private int wallsPosX;
private int wallsPosZ;
void Start()
{
var wi = GetComponent<WallsTest>();
wallsLengthX = (int)wi.lengthX; // I don't have this script so I put in my own values
wallsLengthZ = (int)wi.lengthZ; // I don't have this script so I put in my own values
wallsPosX = (int)wi.wallsStartPosition.x; // I don't have this script so I put in my own values
wallsPosZ = (int)wi.wallsStartPosition.z; // I don't have this script so I put in my own values
}
// Update is called once per frame
void Update()
{
if (currentObjects <= numberOfObjects)
{
float posx = UnityEngine.Random.Range(wallsPosX, wallsPosX + wallsLengthX);
float posz = UnityEngine.Random.Range(wallsPosZ, wallsPosZ + wallsLengthZ);
posx += wallsLengthX * -0.5f;
posz += wallsLengthZ * -0.5f;
float posy = Terrain.activeTerrain.SampleHeight(new Vector3(posx, 0, posz));
GameObject newObject = (GameObject)Instantiate(objectToPlace, new Vector3(posx, posy, posz), Quaternion.identity);
newObject.transform.localScale = new Vector3(5, 5, 5);
//newObject.name = "cube-" + currentObjects; // i change the name so I could verify my script. you can remove this
//check if the spawned object is in the correct place
float x = (wallsPosX + wallsLengthX - newObject.transform.localScale.x) / 2;//you might not need the wallsPosX
float z = (wallsPosZ + wallsLengthZ - newObject.transform.localScale.z) / 2;//you might not need the wallsPosZ
if (System.Math.Abs(newObject.transform.position.x) > x || System.Math.Abs(newObject.transform.position.z) > z)
{
Debug.Log("too far out : remove it : " + newObject.name);
DestroyObject(newObject);
}
else
{
currentObjects += 1;
}
}
if (currentObjects == numberOfObjects)
{
Debug.Log("Generate objects complete!");
}
}
}
But it’s giving me the message “too far out”
I tried to remove the wallsPosX and wallsPosZ at lines 41 and 42 but still same thing “too far out”
In my case i set the WallsTest: Walls Start Position: X = 250 Y = 0 Z = 250
Width = 0
Height = 10
LengthX = 100
LengthZ = 100
Time 1
And the main camera as camera
Both scripts attached to the same empty GameObject and both scripts using same cube as Prefab and Object To Place.