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 greg1992 · Mar 09, 2014 at 10:27 PM · spawning problems

Objects spawning in each other

Hello so i posted up this code the other day trying to figure out the problem i was having with it, it works for the most part perfectly but i'm having some issue with resources spawning within one another.

This problem happens because spawning is always on, as you'll see in the code i'm using a timer to decide when to spawn objects.

I have a fix in mind by splitting the objects up and giving them a separete spawn script with separete points to spawn at.

But i figured they're probably a 100 better ways -i don't know of- of doing that and keeping my code pretty much as it is. I'd like the code to able to check if a spawn point as been used and if so use a different point and if all the points are used stop spawning, i think of the top of my head i could change the spawn array into a list?-maybe-. but i'm unsure i'm still a unity noob and unsure on how to take this forward.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SpawnTest : MonoBehaviour {
 
     public float timer = 0.0f;
     public bool  spawning = false;
     public GameObject[] resources;
     public Transform[] spawn;
 
 
     private GameObject resourcePrefab;
 
 
     void  Update (){
             //check if spawning at the moment, if not add to timer
     
     if(!spawning)
     {
         timer += Time.deltaTime;
     }
             //when timer reaches 2 seconds, call Spawn function
     if(timer >= 2)
     {
             Debug.Log ("SpawnHit");
             StartCoroutine(Spawn());
     }
 }
 
         
     public IEnumerator  Spawn (){
             //set spawning to true, to stop timer counting in the Update function
     spawning = true;
             //reset the timer to 0 so process can start over
     timer = 0;
     Debug.Log ("Timer reset");
             
 
         //select a random spawn point
     int randomPick = Random.Range(0,spawn.Length);
             
         //select a random object from array
     int Clone = Random.Range(0,resources.Length);
             //create the object at point of the location variable
     Instantiate(resources[Clone], spawn[randomPick].position, transform.rotation);
 
             
             //halt script for 1 second before returning to the start of the process
     yield return new WaitForSeconds(1);
             //set spawning back to false so timer may start again
     spawning = false;
     }
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by taylank · Mar 10, 2014 at 04:28 AM

i think of the top of my head i could change the spawn array into a list?-maybe-

Make that definitely. After declaring the list and populating it either in the editor or by code, you can do this:

 //declaring the list here
 public List<Transform> spawn = new List<Transform>(); 
 .
 .
 .
 if (spawn.Count != 0) {
      Instantiate(resources[Clone], spawn[randomPick].position, transform.rotation);
      // remove the point you just used
      spawn.RemoveAt(randomPick);
 }
 else {
      //if all points are used, set spawning to false
      spawning = false;
 }
 
Comment
Add comment · Show 1 · Share
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 greg1992 · Mar 10, 2014 at 06:40 AM 0
Share

Hello taylank thanks for the reply, it kinda works although the spawn points are being removed after each spawn. I don't know if i'm handling the list incorrectly or the removing part incorrectly.

Is there a way of using a spawn point 2 times? so it doesn't spawn an item twice, but if the player picks up an item at the point the spawner has a chance of spawning at the spawn point again.

Code so far

 public class SpawnTest : $$anonymous$$onoBehaviour {
 
     public float timer = 0.0f;
     public bool  spawning = false;
     public GameObject[] resources;
     public List<Transform> spawn = new List<Transform>();
 
 
 
 
     void Start(){
 
 
     }
 
 
     void  Update (){
     
 
     //check if spawning at the moment, if not add to timer
     
     if(!spawning)
     {
         timer += Time.deltaTime;
     }
             //when timer reaches 2 seconds, call Spawn function
     if(timer >= 5)
     {
 
             StartCoroutine(Spawn ());
 
 
     }
 }
 
         
     public IEnumerator  Spawn (){
 
             //set spawning to true, to stop timer counting in the Update function
     spawning = true;
             //reset the timer to 0 so process can start over
     timer = 0;
     Debug.Log ("Timer reset");
 
 
 
         //select a random spawn point
     int randomPick = Random.Range(1, 6);
             
         //select a random object from array
     int Clone = Random.Range(1,2);
         //added code    
     if (spawn.Count !=0)
         {
         //create the object at point of the location variable
     Instantiate(resources[Clone], spawn[randomPick].position, transform.rotation);
     
     spawn.RemoveAt(randomPick);
         
         }    
             
     else
         {
             //set spawning back to false so timer may start again
     spawning = false;
         }
 
     yield return new WaitForSeconds (1);
 
     spawning = false;
     }
 }
 

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

Why does this not work (Involves instantiating objects in response to a collision*) 1 Answer

Side scroller spawns at different Y 1 Answer

Multiplayer: I have an OnTrigger script that isn't correctly responding. Spawning and Destroying GameObjects. 0 Answers

Enemy spawn logic not working 1 Answer

Unity creates unreachable navmesh. How can I remove it? 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