Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Twistyd · Mar 13, 2019 at 10:39 AM · tagspoolingrandomization

In need of some help with objectpooling and randomised tag setting

Good [insert time appropriate salutation] all.

This is a repost of an issue posted to the Help section back in Nov 2018, hoping someone can help now.


~~~~~~~~~~~

~~ 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 :(


I originally posted this in the help section back in November and have since been busy but now I'm free and have still received no help from the help section, trying it here as a last hope. Thanks in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

100 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How would I get data out of a list of lists? (Made from a "List Wrapper") 2 Answers

How can I detect multiple triggers on same gameobject at once? 3 Answers

How can I make a ray stop uppon reaching a trigger? 1 Answer

Unity3D Shooter: Using tags to switch level after killing all enemies. 1 Answer

haw to use suptag in unity c#? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges