Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Apr 26, 2018 at 07:38 AM by tormentoarmagedoom for the following reason:

Too subjective and argumentative

avatar image
0
Question by TarwaterPV · Apr 25, 2018 at 09:02 PM · c#listarraysfindgameobjectswithtag

Why can't I find a GameObject with FindGameObjectsWithTag

Hello:

So I am having problems getting my code to read the correct amount of a specific game object in the scene. The list count or array length always returns zero. It should be showing at least 1 object and it is not even showing that.

         if (GameObject.FindGameObjectsWithTag("RocketUI") != null)
         {            
             foreach (GameObject score in GameObject.FindGameObjectsWithTag("RocketUI"))
             {
                 Debug.LogError("Stepping in...");
                 pScores = score.GetComponent<ScoreCounter>();
                 characterScores.Add(pScores);
             }
             Debug.LogError(characterScores.Count + " RocketUI objects found.");
         }
         else
         {
             Debug.LogError("Cannot find RocketUI objects.");
         }

The code always enters the first condition, so it is obviously not null. However, it never adds anything to the list (characterScores). Also, it seems to not run the Foreach loop at all because that Debug.LogError never runs either. The only thing that shows in the console is the second debug which shows "0 RocketUI objects found." This doesn't really make any sense and I am trying to figure out what is going on. I have been working on this for two days now and am quite frustrated.

What this is supposed to be doing is initializing a reference list of UI objects that are recording scores, then add those scores together and compare them to a score threshold that tells it when to spawn a boss in the level. However, the code cannot locate the object. Here is a pic of the object in the scene I am trying to get: alt text

And yes, that object does have the correct tag assigned, else the code couldn't meet the first condition. I already checked if there were duplicates (there was one but deleting it didn't fix anything) so I am at a loss on what is going on here and what to do. If someone could please help, I would greatly appreciate it. Thank you.

scorecounterobjectinscene.png (80.0 kB)
Comment
Add comment · Show 6
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
avatar image TreyH · Apr 25, 2018 at 09:06 PM 0
Share

From the docs:

Tags must be declared in the tag manager before using them. A UnityException will be thrown if the tag does not exist or an empty string or null is passed as the tag.

Can you link your object's Tag and the tag manager?

avatar image TarwaterPV TreyH · Apr 25, 2018 at 09:29 PM 0
Share

Here you go:

alt text

alt text

scorecountertagobject.png (58.5 kB)
locationintags.png (4.7 kB)
avatar image TreyH · Apr 25, 2018 at 09:08 PM 0
Share

Also, is there a reason you're getting those objects via tag and not something a little more reliable / performant? It's pretty rare that using any variant GameObject.Find is a good idea.

avatar image TarwaterPV TreyH · Apr 25, 2018 at 09:36 PM 0
Share

The reason I am using a .Find is because the objects the spawner is looking for are instantiated dynamically from code. They are not actually in the scene. Part of the reason has to do with how we built our couch multiplayer, we wanted to use the same objects without having to manually place anything through multiple scenes, to save time since we have very very tight deadlines. (tbh, when the game was first being, none of us has any clue on what we were doing, it is our first game. Unfortunately, now we don't have time to go back and rewrite everything more efficiently). Plugging in the object into a field in the inspector wouldn't work because it would be a different instance of what the correct object should be, and it depends on which player has been loaded into the scene. Unfortunately, it is much too long to explain what we were trying to do here. All I am trying to do is find a game object after is instantiated so the code can monitor the score and add it to everyone else's score so we can use a single number to deter$$anonymous$$e when a boss spawns.

avatar image Anne-Pier · Apr 25, 2018 at 09:32 PM 0
Share

Tried the code and it works for me, what if you delete the first if and replace it with the foreach loop ins$$anonymous$$d? also could you provide the entire code if it doesn't work so we can see what pScores is and try it. It's pretty strange though because it's working fine for me, try pausing the game as well and see if the gameobjects lose their tag or are being deleted entirely or something.

avatar image TarwaterPV Anne-Pier · Apr 25, 2018 at 09:43 PM 0
Share

Here you go:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BossSpawner : $$anonymous$$onoBehaviour {
 
     [SerializeField] private int scoreThreshold;
     [SerializeField] private List<GameObject> bosses;
 
     private GameObject hudObject;
 
     private List<ScoreCounter> characterScores = new List<ScoreCounter>();
     private ScoreCounter pScores;
 
     private int baseScore;
     private int totalScore;
     private int iteration;
 
     private void Start ()
     {
         baseScore = 0;
         iteration = 0;
         
         if (GameObject.FindGameObjectsWithTag("RocketUI") != null)
         {            
             foreach (GameObject score in GameObject.FindGameObjectsWithTag("RocketUI"))
             {
                 Debug.LogError("Stepping in...");
                 pScores = score.GetComponent<ScoreCounter>();
                 characterScores.Add(pScores);
             }
             Debug.LogError(characterScores.Count + " RocketUI objects found.");
         }
         else
         {
             Debug.LogError("Cannot find RocketUI objects.");
         }
         // StartCoroutine(Score$$anonymous$$onitor());
     }
 
     // Update is called once per frame
     void Update () {
         
     }
 
     private IEnumerator Score$$anonymous$$onitor()
     {
         yield return new WaitForSeconds(0.1f);
         int scorePools = characterScores.Count;        
         int i = 0;        
         while (i < scorePools)
         {            
             baseScore += characterScores[i].GetComponent<ScoreCounter>().characterScore;
             i++;
         }
 
         totalScore = baseScore;
 
         if (totalScore == (scoreThreshold * scorePools))
         {
             SpawnBoss();
         }        
     }
 
     private void SpawnBoss()
     {
         iteration++;
 
         GameObject bossObject = Instantiate(bosses[Random.Range(0, characterScores.Count)]) as GameObject;
         bossObject.transform.position = transform.position;
         bossObject.transform.rotation = transform.rotation;
     }
 }
 

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by TarwaterPV · Apr 25, 2018 at 11:43 PM

Ok, I decided to just rewrite the entire script and came up with a far more simpler solution. I decided to have the score counters send their points to the monitor directly via events instead. Then the monitor uses a coroutine to check when the threshold has been reached. So far it works good. Thank you everyone for your assistance.

Oh, here is the code if anyone is curious, it is a much more simple set up:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ScoreMonitor : MonoBehaviour {
 
     [SerializeField] private int bossScoreThreshold;
     [SerializeField] private List<GameObject> bosses = new List<GameObject>();
 
     static public ScoreMonitor monitor;
 
     private int iterator;
 
     private bool setCoroutine;
 
     private int totalScore;
     public int TotalScore { set { totalScore = value; } }
 
     private int playerAmount;
     public int PlayerAmount { set { playerAmount = value; } }
 
     // Use this for initialization
     private void Awake()
     {
         monitor = this;
     }
 
     void Start ()
     {
         iterator = 1;
         totalScore = 0;
         setCoroutine = true;
         StartCoroutine(MonitorScore());
     }
 
     private void OnEnable()
     {
         ScoreCounter.Score += AddScore;
         BossHealth_Offline.Death += BeginCoroutine;
     }
 
     private void OnDisable()
     {
         ScoreCounter.Score -= AddScore;
         BossHealth_Offline.Death -= BeginCoroutine;
     }
 
     private void AddScore(int score)
     {
         totalScore += score;
     }
 
     private IEnumerator MonitorScore()
     {
         while (setCoroutine)
         {
             if (totalScore >= ((bossScoreThreshold * playerAmount) * iterator))
             {
                 SpawnBoss(Random.Range(0, bosses.Count));
             }
             yield return new WaitForSeconds(0.1f);
         }
     }
 
     private void SpawnBoss(int boss)
     {
         iterator++;
         setCoroutine = false;
 
         GameObject enemyBoss = Instantiate(bosses[boss]) as GameObject;
         enemyBoss.transform.position = transform.position;
         enemyBoss.transform.rotation = transform.rotation;
     }
 
     private void BeginCoroutine(bool start)
     {
         setCoroutine = start;
         StartCoroutine(MonitorScore());
     }
 }
Comment
Add comment · Show 1 · Share
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
avatar image Anne-Pier · Apr 26, 2018 at 08:42 AM 0
Share

Glad it worked out :), also for future reference, if you instantiate an object, you can instantly set it to a variable as well. So if you used Instantiate, you could've also added it to the list from there :).

avatar image
0

Answer by Anne-Pier · Apr 25, 2018 at 09:36 PM

You could try using GameObject.Find("ScoreCount") instead if you only have 1 object with this tag, or even store it in a variable. It's weird that it doesn't work because it goes into the first if, try creating a new script and build it up and see when it goes wrong if you're really interested, or try it on an empty scene with the same script and see what happens. Debugging like this should get you to the problem.

Comment
Add comment · Show 1 · Share
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
avatar image TarwaterPV · Apr 25, 2018 at 09:47 PM 0
Share

Yeah, the problem with using just a .Find("ScoreCount") is that it needs to look for the possibility of there being up to four Score Count objects due to multiplayer, hence why I need a list.

Also, this is a new script, I built it from scratch lol. I could try rebuilding it again, not for sure that will work. It is dependent upon a UI being present though so it will be hard to test it in a new scene. I will see what I can do.

Follow this Question

Answers Answers and Comments

537 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 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 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 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 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 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

Get Childrens to new array from perents array. 1 Answer

How Do I Create a List of Two Strings? C# 1 Answer

How can I save this objects ? 0 Answers

Need help to generate a random assortment of 4 buttons - C# 0 Answers

compare string to string array[] 2 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