Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mjhurtado1 · Jun 19, 2015 at 06:46 AM · c#instantiateraycastrandomlocation

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Jun 19, 2015 at 06:54 AM 1
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random array issue C# 2 Answers

instantiate prefabs y+1 from last instintiated prefab 1 Answer

Spawn enemies so they aren't spawned on top of each other (C#) 1 Answer

Keep random Instantiation from spawning ontop of other objects 1 Answer

Offseting insantiated object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges