- Home /
Find a random location, spawn an object there, do it again.
I have a random world that is loaded in every time the game starts, but the world has no objects. I decided to keep the random theme going and have the objects that spawn in be random too, and in random locations.
This script is suppose to find a random location within certain bounds, check if you can spawn an object there, spawn the object, then do it again if you haven't exceeded the max object limit.
The problem is (I think), the raycast doesn't hit anything, and it goes into an endless loop and crashes the game.
The Code: using UnityEngine; using System.Collections;
public class ObjectSpawner : MonoBehaviour {
// Raycasting stuff...
public GameObject raycaster;
RaycastHit hit;
Ray ray;
// Spawning Bounds
public float maxX = 2000;
public float minX = 0;
public float maxZ = 2000;
public float minZ = 0;
public float y = 5;
// Max objects / Current amount of objects
public int maxObjects;
int currentObjects = 0;
// The set of random objects that can be spawned in
public GameObject[] objects;
// The teleporting location
Vector3 location;
// Start
private void Start(){
Debug.Log("Spawning objects in random locations...");
ChangeLocation();
}
// Changes the location and spawns in the objects
private void ChangeLocation(){
// Sets the random location
location = new Vector3 (Random.Range(minX, maxX), y, Random.Range(minZ, maxZ));
Debug.Log("New spawning location: X/" + location.x + " Y/" + y + " Z/" + location.z);
// Teleports this object to the random location
transform.position = location;
// Raycast stuff? (Not too sure if this is correct or not)
ray = new Ray (raycaster.transform.position, Vector3.down);
// Check if it hit something
if (Physics.Raycast (ray, out hit, 100)) {
// Check if it hit one of the random rooms that spawned in
if (hit.collider.tag == "Room") {
// Checks if you can spawn more objects
if (currentObjects < maxObjects) {
currentObjects++;
Instantiate (objects [Random.Range (0, objects.Length)], hit.transform.position, Quaternion.identity);
ChangeLocation ();
} else {
Debug.Log ("Can not spawn any more objects, stopping the proccess...");
}
}else{
if (currentObjects < maxObjects){
ChangeLocation();
Debug.Log ("Couldn't find a location to spawn! Trying again...");
}else{
Debug.Log ("Can not spawn any more objects, stopping the proccess...");
}
}
} else {
if (currentObjects < maxObjects){
ChangeLocation();
Debug.Log ("Couldn't find a location to spawn! Trying again...");
}else{
Debug.Log ("Can not spawn any more objects, stopping the proccess...");
}
}
}
}
Any help would be greatly appreciated! :D
I would suggest to put break points in your code and run your script step by step.
Other solution, but a Debug.LogError ins$$anonymous$$d of a Debug.Log("New spawning location...") and click on the "Error Pause" button in the Console Tab of Unity, you will be able to see where your raycaster is located in the Scene view.
Other helpful tool :
public static void DrawRay(Vector3 start, Vector3 dir, Color color = Color.white, float duration = 0.0f, bool depthTest = true);
http://docs.unity3d.com/ScriptReference/Debug.DrawRay.html
Put this just after you define your ray. Thus, you will be able to visualize the ray you cast.