- Home /
Need help with my #c script to Generate 2 Obstacles
This code bellow is working for the one Obstacle1 prefab this is what happens when its working for obstacle1. obstacle1 generates off screen on right moves left then off screen then disappears and continues back to the beginning.
Im new in making games... I have this script GenerateObstacles.cs file that is working and it automatically generates Obstacle1 prefab. I've been trying to figure out how to add "Obstacle2" with a different float wait time. I've tried to make it work. Then when i go to test run the game the screen blinks and dose nothing. Need help please and thank you.
using UnityEngine;
using System.Collections;
public class GenerateObstacles : MonoBehaviour {
void Start () {
StartCoroutine("CreateObstacle");
}
IEnumerator CreateObstacle() {
float waitTime = 2.5f;
while(true) {
ObjectPool.instance.GetObjectForType("Obstacle1", true);
yield return new WaitForSeconds(waitTime);
}
}
}
there is something seriously wrong with your co routine, you do realize a while(true) statement will loop indefinitely right? in this case every 2.5 seconds ... forever,
also can you show me how you've we been trying to make "obstacle2" appear? and what is in this object Pool class? you clearly have some sort of singleton going on there. and some casting/ get reference function...
the main reason Obstacle2 doesn't appear because you never reference it at all. what do you mean by "add"? do you mean instantiate it? or add it to the pool so it can be used.
Are you new to C# program$$anonymous$$g as well as Game Development? if so did you take a tutorial on Object pooling ?
http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling
basically your co routine is an endless loop (which is bad for creating an object).
I can't help you unless you tell me how ObjectPool class and GetObjectForType function works
Thank you for your quick reply. I posted my ObjectPool.cs. Obstacle.cs I assign to my object obstacle1 prefab. Just waiting for approval by a moderator so you can view it.
Answer by jeffreymarasigan · Jan 25, 2015 at 08:49 PM
Thank you for the quick reply and yes I am new to C# programing. This is my ObjectPool.cs
using System.Collections;
using System.Collections.Generic;
public class ObjectPool : MonoBehaviour {
public static ObjectPool instance;
public GameObject[] objectPrefabs;
public List<GameObject>[] pooledObjects;
public int[] amountToBuffer;
protected GameObject containerObject;
void Awake() {
instance = this;
}
void Start() {
containerObject = new GameObject("ObjectPool");
pooledObjects = new List<GameObject>[objectPrefabs.Length];
int i = 0;
for(int j = 0; j < objectPrefabs.Length; j++) {
pooledObjects[i] = new List<GameObject>();
int bufferAmount = amountToBuffer[i];
for(int n = 0; n < bufferAmount; n++) {
GameObject newObj = Instantiate(objectPrefabs[j]) as GameObject;
newObj.name = objectPrefabs[j].name;
PoolObject(newObj);
}
i++;
}
}
public GameObject GetObjectForType(string objectType, bool onlyPooled) {
for(int i = 0; i < objectPrefabs.Length; i++) {
GameObject prefab = objectPrefabs[i];
if(prefab.name == objectType) {
if(pooledObjects[i].Count > 0) {
GameObject pooledObject = pooledObjects[i][0];
pooledObjects[i].RemoveAt(0);
pooledObject.transform.parent = null;
pooledObject.SetActive(true);
return pooledObject;
} else if(!onlyPooled) {
return Instantiate(objectPrefabs[i]) as GameObject;
}
break;
}
}
return null;
}
public void PoolObject(GameObject obj) {
for(int i = 0; i < objectPrefabs.Length; i++) {
if(objectPrefabs[i].name == obj.name) {
obj.SetActive(false);
obj.transform.parent = containerObject.transform;
pooledObjects[i].Add(obj);
return;
}
}
}
}
And this is Obstacle.cs I assign to my object obstacle1 prefab.
using System.Collections;
public class Obstacle : MonoBehaviour {
public Vector3 velocity = new Vector3(-2.5f, 0);
public AudioClip sound;
private Transform cachedTransform;
private bool hasEnteredTrigger = false;
void Awake() {
cachedTransform = transform;
}
void Update() {
cachedTransform.Translate(velocity * Time.smoothDeltaTime);
if(!isVisible()) {
Deactivate();
}
}
void OnEnable() {
cachedTransform.position = new Vector3(11, Random.Range(-3.0f, 3.0f), 5);
}
void OnDisable() {
cachedTransform.position = new Vector3(-9999, 0, 5);
hasEnteredTrigger = false;
}
bool isVisible() {
bool result = true;
Vector2 screenPosition = Camera.main.WorldToScreenPoint(cachedTransform.position);
if(screenPosition.x < -100) {
result = false;
}
return result;
}
void Deactivate() {
ObjectPool.instance.PoolObject(gameObject);
}
void OnTriggerEnter2D(Collider2D other) {
if(hasEnteredTrigger == false && other != null && other.CompareTag("Player")) {
Score.TotalScore += 1;
AudioSource.PlayClipAtPoint(sound, new Vector3(0, 0, 0), 1.0f);
hasEnteredTrigger = true;
}
}
}
Thank you for your time and help...
Just by quickly looking at your pool class I could tell that your flag "onlyPooled", if set to true and if you dont buffer the Obstacle2 prefab in your hierarchy you wont be able to create any instances of it.
I would recommend getting rid of this flag and let your pool decide whether to create a new element or not. everything else looks right. (but highly inefficient)
Answer by syedzia · Oct 13, 2015 at 01:11 PM
using UnityEngine; using System.Collections;
public class genarate : MonoBehaviour {
public GameObject rocks;
// Use this for initialization
void Start()
{
InvokeRepeating("CreateObstacle", 1f, 1.5f);
}
void CreateObstacle()
{
Instantiate (rocks);
}
}