- Home /
Platforms disappear when adding another powerup
Ok so I've made a working endless runner and i would like to add some more powerups/enemies other than the booster that is currently working.
When I add the code for an enemy the platforms disappear from the game
the code for generating platforms is below and below that is the code for the enemy object, the lines i've commented out are what i thought should make an enemy object appear that should kill the player on contact.
Any help would be greatly appreciated
using UnityEngine;
using System.Collections.Generic;
public class PlatformManager : MonoBehaviour {
public Transform prefab;
public int numberOfObjects;
public float recycleOffset;
public Vector3 startPosition;
public Vector3 minSize, maxSize, minGap, maxGap;
public float minY, maxY;
public Material[] materials;
public PhysicMaterial[] physicMaterials;
public Booster booster;
//public Enemy enemy;
private Vector3 nextPosition;
private Queue<Transform> objectQueue;
void Start () {
GameEventManager.GameStart += GameStart;
GameEventManager.GameOver += GameOver;
objectQueue = new Queue<Transform>(numberOfObjects);
for (int i = 0; i < numberOfObjects; i++) {
objectQueue.Enqueue((Transform)Instantiate(
prefab, new Vector3(0f, 0f, -100f), Quaternion.identity));
}
enabled = false;
}
void Update () {
if(objectQueue.Peek().localPosition.x + recycleOffset < Runner.distanceTraveled){
Recycle();
}
}
private void Recycle () {
Vector3 scale = new Vector3(
Random.Range(minSize.x, maxSize.x),
Random.Range(minSize.y, maxSize.y),
Random.Range(minSize.z, maxSize.z));
Vector3 position = nextPosition;
position.x += scale.x * 0.5f;
position.y += scale.y * 0.5f;
booster.SpawnIfAvailable(position);
//enemy.SpawnIfEnemyAvailable(position);
Transform o = objectQueue.Dequeue();
o.localScale = scale;
o.localPosition = position;
int materialIndex = Random.Range(0, materials.Length);
o.renderer.material = materials[materialIndex];
o.collider.material = physicMaterials[materialIndex];
objectQueue.Enqueue(o);
nextPosition += new Vector3(
Random.Range(minGap.x, maxGap.x) + scale.x,
Random.Range(minGap.y, maxGap.y),
Random.Range(minGap.z, maxGap.z));
if(nextPosition.y < minY){
nextPosition.y = minY + maxGap.y;
}
else if(nextPosition.y > maxY){
nextPosition.y = maxY - maxGap.y;
}
}
private void GameStart () {
nextPosition = startPosition;
for(int i = 0; i < numberOfObjects; i++){
Recycle();
}
enabled = true;
}
private void GameOver () {
enabled = false;
}
}
using UnityEngine;
public class Enemy : MonoBehaviour {
public Vector3 offset, rotationVelocity;
public float recycleOffset, spawnChance;
void Start () {
GameEventManager.GameOver += GameOver;
gameObject.SetActive(false);
}
void Update () {
if(transform.localPosition.x + recycleOffset < Runner.distanceTraveled){
gameObject.SetActive(false);
return;
}
transform.Rotate(rotationVelocity * Time.deltaTime);
}
void OnTriggerEnter () {
GameEventManager.TriggerGameOver ();
gameObject.SetActive(false);
}
public void SpawnIfEnemyAvailable (Vector3 position) {
if(gameObject.activeSelf || spawnChance <= Random.Range(0f, 100f)) {
return;
}
transform.localPosition = position + offset;
gameObject.SetActive(true);
}
private void GameOver () {
gameObject.SetActive(false);
}
}
Your answer
Follow this Question
Related Questions
Android Build fails 3 Answers
CommandInvokationFailure: Failed to re-package resources. 0 Answers
Game won't build 1 Answer
How to resolve the error on build "drawable/app_banner not found in AndriodManifest.xml:57" 1 Answer
Health on photon 3 Answers