Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Apr 25, 2018 at 11:51 PM by S_jay1 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by S_jay1 · Dec 28, 2017 at 05:27 PM · transformprefabpooling

Why am I spawning a ton of prefabs in one area?

I'm trying to create a really basic 3d runner type of game, and I'm trying to instantiate different prefabs that I have created to use as the floor. I'm trying to use a game object with an object pooler script to spawn a prefab that changes into one of the different prefabs that I'm trying to use as the floor. I'm trying to make it so that the spawner prefab/quad changes in its z position every time it is spawned again so that the floor will be spawned further as the player moves further. The problem is that the prefabs are being spawned in the same position and a ton of them are being instantiated. These are my two scripts:

Object Pooling:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ObjectPooler : MonoBehaviour {

 public static ObjectPooler current;
 public GameObject pooledObject;
 public int pooledAmount = 10;
 public bool willGrow = true;

 List<GameObject> pooledObjects;


 void Start()

 {
     pooledObjects = new List<GameObject> ();
     for (int i = 0; i < pooledAmount; i++) {
         GameObject obj = (GameObject)Instantiate (pooledObject);
         obj.SetActive (false);
         pooledObjects.Add (obj);
     }

 }

 public GameObject GetPooledObject()
 {
     for (int i = 0; i < pooledObjects.Count; i++) 
         {
             if (!pooledObjects [i].activeInHierarchy) 
             {
                 return pooledObjects [i];
             }

         }

         if(willGrow)
         {
             GameObject obj = (GameObject)Instantiate (pooledObject);
             pooledObjects.Add (obj);
             return obj;

     }
         return null;
 }

 }

Spawning/Instantiating the Floor:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class SpawningPrefabs : MonoBehaviour {

 List<GameObject> prefabList = new List<GameObject>();
 public GameObject Prefab0;
 public GameObject Prefab1;
 public GameObject Prefab2;
 public GameObject Prefab3;
 public GameObject Prefab4;


 public float previousZ;

 void Start()
 {
     prefabList.Add (Prefab0);
     prefabList.Add (Prefab1);
     prefabList.Add (Prefab2);
     prefabList.Add (Prefab3);
     prefabList.Add (Prefab4);
 }

 void Update(){
     CreatePrefab ();
 }

 void CreatePrefab ()
 {
     int prefabIndex = UnityEngine.Random.Range(0,5);
     Instantiate(prefabList[prefabIndex], new Vector3(0.0f, 0.0f, previousZ + 10.0f), Quaternion.identity);

     changeZ ();

 }
 void changeZ()
 {
     previousZ = GetComponent<Rigidbody>().transform.position.z; 
 }

}

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

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by Elisvaldo · Dec 28, 2017 at 05:32 PM

You are calling the CreatePrefab() in the Update() function. So it will instantiate one every frame!

Comment
Add comment · Show 2 · 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 S_jay1 · Dec 29, 2017 at 03:59 PM 0
Share

Where should I put the CreatePrefab()? I tried putting it in the Start() function but that spawns one and nothing else happens.

avatar image Elisvaldo S_jay1 · Dec 30, 2017 at 02:41 PM 1
Share

That is a good question, and one where the fun of game development starts :) If you are making an endless runner, then I'd say you wan to instantiate sometime around when the player runs every X meters. Or maybe instantiate every time the player passes one obstacle. $$anonymous$$aybe you want to instantiate less obstacles if the player is running faster, and more obstacles when it is slower. $$anonymous$$aybe every Y meters you want to make a "hard section" and you instantiate more in there than in the other areas. You will have to figure out this one by yourself, as it depend a lot on what your game is all about!

avatar image
0

Answer by S_jay1 · Dec 30, 2017 at 03:55 PM

Thank you for the response! Could you give me a really simple/basic code structure of what instantiating every Y meters would look like? It sounds like exactly what I need but I'm not sure how to do that.

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 Elisvaldo · Dec 31, 2017 at 02:53 AM 1
Share

something like:

 var nextDistance = 5;
 
 void Update()
 {
     if(player.transform.position.x > distance)
     {
        InstantiateObject();
        nextDistance += player.transform.position.x;
     }
 }

Follow this Question

Answers Answers and Comments

105 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 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 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 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 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

Saving customized transform in game 1 Answer

Why Transform and not Prefab? 2 Answers

transforming a prefab randomly at runtime 3 Answers

Can't fire a projectile from an object pool 1 Answer

Modify only the y axis in created animation 0 Answers


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