0
\$\begingroup\$

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' toObjectPooler'".

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.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

GameObject karhu = enemyPool = enemyPool.GetPooledObject ();

Why are you assigning enemyPool to karhu? enemyPool is of type ObjectPooler and karhu is of type GameObject, which is causing the problem.

\$\endgroup\$
2
  • \$\begingroup\$ Oh. I see. I had an extra enemy pool there. Oh and all of the EnemyGenerator class is useless. Oops. But the problem still stands. \$\endgroup\$ Commented Mar 23, 2016 at 6:01
  • \$\begingroup\$ I edited the question. @Evil Tak \$\endgroup\$ Commented Mar 23, 2016 at 6:23
0
\$\begingroup\$

Delete every reference to EnemyGenerator class. It should look like this.

using UnityEngine; using System.Collections;

public class PlatformGenerator : MonoBehaviour {

public GameObject thePlatform;
public Transform generationPoint;
public float distanceBetween;

private float platformWidth;

public ObjectPooler theObjectPool;

public float randomEnemyThreshold;
public ObjectPooler enemyPool;

// Käytä alkuarvon alustamiseen
void Start () {
    platformWidth = thePlatform.GetComponent<BoxCollider2D>().size.x;


}

// 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);
        }
    }

}

}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .