I'm trying to make two obstacles that are different sized to come in a random order in one straight line where the player is going. Doesn't have to be with object pooling.
It seems that the EnemyGenerator class is useless and all can be done in platformGenerator.
using UnityEngine; using System.Collections;
public class EnemyGenerator : MonoBehaviour {
public ObjectPooler enemyPool;
public float distanceBetweenEnemies;
public void SpawnEnemies (Vector3 startPosition)
{
GameObject karhu = enemyPool = enemyPool.GetPooledObject ();
karhu.transform.position = startPosition;
karhu.SetActive (true);
//GameObject punakuono = enemyPool = enemyPool.GetPooledObject ();
//punakuono.transform.position = new Vector3(startPosition.x - distanceBetweenEnemies, startPosition.y, startPosition.z);
//punakuono.SetActive (true);
}
}
This also returns the error: "Assets/Scripts/EnemyGenerator.cs(12,36): error CS0029: Cannot implicitly convert type UnityEngine.GameObject' to
ObjectPooler'".
Edit: I had an extra enemyPool and the class is useless.
My game does spawning with a generation point that is stuck in the camera and when the player runs the generation point goes forward and spawns flatground ahead of him.
Ask me for more example code if you need.
Edit 1: Here is the PlatformGenerator that also spawns other objects.
using UnityEngine; using System.Collections;
public class PlatformGenerator : MonoBehaviour {
public GameObject thePlatform;
public Transform generationPoint;
public float distanceBetween;
private float platformWidth;
public ObjectPooler theObjectPool;
private EnemyGenerator theEnemySpawner;
public float randomEnemyThreshold;
public ObjectPooler enemyPool;
// Käytä alkuarvon alustamiseen
void Start () {
platformWidth = thePlatform.GetComponent<BoxCollider2D>().size.x;
theEnemySpawner = FindObjectOfType<EnemySpawner> ();
}
// Päivitys tulee joka ruutu
void Update () {
if (transform.position.x < generationPoint.position.x)
{
transform.position = new Vector3 (transform.position.x + platformWidth + distanceBetween, transform.position.y, transform.position.z);
//Instantiate (thePlatform, transform.position, transform.rotation);
GameObject newPlatform = theObjectPool.GetPooledObject();
newPlatform.transform.position = transform.position;
newPlatform.transform.rotation = transform.rotation;
newPlatform.SetActive (true);
if (Random.Range (0f, 100f) < randomEnemyThreshold)
{
GameObject newEnemy = enemyPool.GetPooledObject();
Vector3 enemyPosition = new Vector3 (0f, 0.5f, 0f);
newEnemy.transform.position = transform.position + enemyPosition;
newEnemy.transform.rotation = transform.rotation;
newEnemy.SetActive(true);
}
}
}
}
Edit 3: I found the problem.