Issues with randomised objectpool, tag setting and re-setting, general noob issues
Good [insert time appropriate salutation] all.
~~~~~~~~~~
~~ TL:DR ~~
~~~~~~~~~~
I'm a noob and I'm stuck. My bubbles (GameObjects) won't work properly, they won't set their tag to what is needed when needed, tried a couple things, they didn't work. Don't drink and develop!
~~~~~~~~~~~
~~ Preface ~~
~~~~~~~~~~~
I'm a noob with very limited experience coding, and in need of an assist. I'm not adverse to a little bit of RTFM but sometimes I'm not entirely sure I'm picking up what's being put down as it were. I'm not that learned in the design pattern and pipeline/work-flow department yet and have only just seen the power of serialization though still don't have a grasp of it. So when helping me (if you can) please keep it somewhat simple, I'm happy to go learn some more on the non/pre coding stuff too like design patterns and pipeline so if you can point me in a direction that doesn't show me them in C++ you'll have my thanks but that's not the main reason I'm here, see below.
~~~~~~~~~~~~~~~~~
~~ Game Concept ~~
~~~~~~~~~~~~~~~~~
Player pops bubbles which match a 'safe' colour chosen at random, if player hits a 'non safe' bubble they lose a life. The 'safe' bubble is changed using a random time of between blah and blah. Simple right? Apparently not for me.
~~~~~~~~~~~~~
~~ The Issue ~~
~~~~~~~~~~~~~
So I have my objectpool, a List, which is populated by an array of GameObjects. The GameObjects which populate said array are public and get set in Unity Inspector. I have the bubbles spawning randomly from the pool as I want them too. I have the randomisation working. I could be wrong though.
What's going wrong is that if the safe bubble colour changes to lets say purple and a purple bubble already exists/was spawned before the safe colour change, this bubble is not seen as safe.
I also have an issue in that once a bubble is marked as safe it will forever then be safe. I tried to re-set the bubble tag when the bubble gets popped but this had the result of setting all bubbles of that colour to unsafe even though they were chosen as safe
~~~~~~~~~~~~~~~
~~ The Script(s) ~~
~~~~~~~~~~~~~~~
Spawner/Pooler
This script also contains the spawner's spawn location movement and timings.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MasterSpawner : MonoBehaviour{
public GameObject chosenBubble;
public int index;
public int randomIndex;
public bool willGrow = true;
public List<GameObject>[] pooledObjects;
public GameObject[] bubblePrefabs;
public GameObject purpleBubble;
public GameObject blueBubble;
public GameObject greenBubble;
public GameObject redBubble;
public GameObject yellowBubble;
public GameObject pinkBubble;
public float timeBetweenSpawns;
public float speed = 1.0f;
public float swapTime;
public Transform spawnLoc;
public Vector3 Position1;
public Vector3 Position2;
public Image BubbleToPopImg;
public Sprite red;
public Sprite blue;
public Sprite purple;
public Sprite pink;
public Sprite green;
public Sprite yellow;
void Awake(){
bubblePrefabs = new GameObject[6];
bubblePrefabs [0] = purpleBubble;
bubblePrefabs [1] = blueBubble;
bubblePrefabs [2] = greenBubble;
bubblePrefabs [3] = redBubble;
bubblePrefabs [4] = yellowBubble;
bubblePrefabs [5] = pinkBubble;
chosenBubble = bubblePrefabs[index];
swapTime = Random.Range (4f,6f);
Randomiser ();
timeBetweenSpawns = Random.Range (0.6f,2.2f);
InvokeRepeating ("Spawn", 0f, timeBetweenSpawns);
pooledObjects = new List<GameObject>[bubblePrefabs.Length];
for (int i = 0; i < bubblePrefabs.Length; i++){
pooledObjects[i] = new List<GameObject>();
}
}
public GameObject GetPooledObject(){
randomIndex = Random.Range(0, pooledObjects.Length);
for (int i = 0; i < pooledObjects[randomIndex].Count; i++){
GameObject go = pooledObjects[randomIndex][i];
if (!go.activeInHierarchy){
return go;
}
}
if (willGrow){
GameObject obj = (GameObject)Instantiate(bubblePrefabs[randomIndex]);
pooledObjects[randomIndex].Add(obj);
return obj;
}
return null;
}
public void Spawn(){
GameObject bubbles = GetPooledObject ();
if(bubbles != null){
bubbles.transform.position = spawnLoc.transform.position;
bubbles.transform.rotation = spawnLoc.transform.rotation;
bubbles.SetActive(true);
}
}
void Update(){
transform.position = Vector3.Lerp (Position1, Position2, Mathf.PingPong(Time.time*speed, 1.0f));
if(timeBetweenSpawns<=0){
timeBetweenSpawns = Random.Range (0.6f,3f);
}
swapTime -= Time.deltaTime;
if(swapTime <= 0){
Randomiser ();
swapTime = Random.Range (3f,6f);
}
switch(index){
case 5: BubbleToPopImg.sprite = pink;
chosenBubble.tag = "Safe";
break;
case 4: BubbleToPopImg.sprite = yellow;
chosenBubble.tag = "Safe";
break;
case 3: BubbleToPopImg.sprite = red;
chosenBubble.tag = "Safe";
break;
case 2: BubbleToPopImg.sprite = green;
chosenBubble.tag = "Safe";
break;
case 1: BubbleToPopImg.sprite = blue;
chosenBubble.tag = "Safe";
break;
case 0: BubbleToPopImg.sprite = purple;
chosenBubble.tag = "Safe";
break;
}
chosenBubble = bubblePrefabs[index];
}
public void Randomiser(){
index = randomIndex;
chosenBubble = bubblePrefabs[index];
}
}
Popper Script
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Popper : MonoBehaviour{
public float fireRate = 1.6f;
public float speed = 20;
float timeLeft = 5f;
public int damagePerShot = 1;
int shootableMask;
float effectsDisplayTime =1.6f;
float timer;
public MasterSpawner masterSpawner;
void Awake (){
shootableMask = LayerMask.GetMask ("Shootable");
}
void Update(){
timer += Time.deltaTime;
if (Input.GetMouseButtonDown (0)) {
PopShot ();
}
}
void PopShot () {
timer = 1.2f;
Ray toMouse = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
bool didHit = Physics.Raycast(toMouse, out hitInfo, 500.0f,shootableMask);
if (didHit && hitInfo.collider.gameObject.tag == "Bubble") {
Bubble bubbleHealth = hitInfo.collider.GetComponent<Bubble> ();
if (bubbleHealth != null) {
bubbleHealth.PopDamage (1, hitInfo.point);
}
GM.playerLives -= 1;
}
if (didHit && hitInfo.collider.gameObject.tag == "Safe") {
Bubble bubbleHealth = hitInfo.collider.GetComponent<Bubble> ();
if (bubbleHealth != null) {
bubbleHealth.PopDamage (1, hitInfo.point);
}
}
}
}
The popper script is pretty much that as is shown in the Unity Survival Shooter tutorial with some tweaks
and finally the Bubble script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bubble : MonoBehaviour {
public MasterSpawner masterSpawner;
public int startingHealth = 1;
public int currentHealth;
public int scoreValue = 10;
public float popPoint;
public float spinSpeed;
public float riseSpeed;
public AudioSource popAudio;
bool isPopped;
// Use this for initialization
void Start () {
/*
Debug.Log ("I'm a "+ gameObject +
" The image is "+ masterSpawner.BubbleToPopImg.sprite.ToString() +
" ~~~ my tag is " + gameObject.tag);
*/
riseSpeed = Random.Range (0.8f,1.8f) * Time.deltaTime;
popPoint = Random.Range (10f,20f);
currentHealth = startingHealth;
}
public void PopDamage(int amount, Vector3 hitPoint){
if (isPopped)
return;
currentHealth -= amount;
if(currentHealth <= 0 ){
popAudio.Play ();
GM.score += 10;
popAudio.Play ();
gameObject.SetActive(false);
}
}
// Update is called once per frame
public void Update () {
transform.Translate (Vector3.up * riseSpeed, Space.World);
transform.Rotate (Vector3.right * 2* Time.deltaTime);
transform.Rotate (Vector3.up * spinSpeed* Time.deltaTime);
if(gameObject.transform.position.y >= popPoint ){
gameObject.SetActive(false);
popPoint = Random.Range (10f,20f);
}
}
}
The bubble script controls the ascent of the bubble, score, auto clearance of the bubble at a certain height and a basic modified version of the Enemy Health script also from the Unity Survival Shooter tutorial.
I added a Debug.Log in the bubbles Start function so each time a bubble spawns it outputs; the gameObject (name of itself); the currently selected safe bubble; this bubbles' tag (to see if it matches the safe colour)
There is also a small issue of coding while inebriated to contend with. In a fit of alcohol infused inspiration I tried to 'fix' things last night and as such I may have made things worse :( Any help will be gratefully received as this has been a bane for a few days now. Thanks in advance.
4 $$anonymous$$onths later and still not resolved, got busy too so never really got a chance to go insane resolving this. Any help is appreciated at this point as I really would love to know what's going wrong and how I can get it sorted
Your answer
Follow this Question
Related Questions
Multiple colliders index problem,i have multi colliders, which collider is my? 2 Answers
Find closest object with a certain tag 2 Answers
Usage of multiple tags or some other workaround? 1 Answer
Unable to use tags on children of a gameobject. 0 Answers
How do I stop a child from taking the parent's tag? 1 Answer