Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[C#] Weird thing happening when FindGameObjectsWithTag after instantiation

Discussion in 'Scripting' started by lkarus, Jul 10, 2015.

  1. lkarus

    lkarus

    Joined:
    Feb 25, 2015
    Posts:
    51
    So currently in my Unity project, I can place enemies on the map with a simple mouse click.

    I instantiate them by using the following code.
    Code (CSharp):
    1. GameObject enemy = (GameObject)Instantiate(m_enemyPrefab);
    And after that, I need to do some calculation with the enemy's data, so after I press a button on my UI, I use the GameObject.FindGameObjectsWithTag() function to get a list of enemies.

    Currently I have only instantiated one enemy. And when I call the FindGameObjectsWithTag function, I get an array with 2 gameobjects of the enemy. However, on my game screen and my inspector, there is only 1 enemy object instantiated in the world.
    11.png

    Am I doing something wrong here? Not sure why I'd get the prefab that I use to instantiate the enemies (The prefab is called char_robotGuard)

    Edit : So I played around with this and this issue did not occur when I placed an enemy, clicked on the scene (Not the Game tab, the scene tab), and pressed the UI button.
     
    Last edited: Jul 10, 2015
  2. magnite

    magnite

    Joined:
    Dec 12, 2012
    Posts:
    125
    From the looks of what you provided the char_robotGuard(Clone) is the GameObject that you are instantiating, and the other GameObject, GameObjectEnemy, is already in your scene. Could you provide more of the code that you are working with, like when you instantiate and what happens afterward that point?
     
  3. lkarus

    lkarus

    Joined:
    Feb 25, 2015
    Posts:
    51
    Yes I pass a reference of the prefab as a public and use it to instantiate the objects.

    So, first I'll take a mouse input from the player. I will then check if that grid is an already occupied grid. If it isnt, I'll then place my enemy using the following code:
    Code (CSharp):
    1.             //if not, we will palce a robot there
    2.             GameObject enemy = (GameObject)Instantiate(m_enemyPrefab);
    3.             enemy.name = "GameObjectEnemy";
    4.             enemy.tag = "Enemy";
    5.             enemy.transform.position = pos;
    6.  
    7.             enemy.layer = LayerMask.NameToLayer("Enemy");
    8.  
    9.             enemy.transform.parent = m_enemyGameObject.transform;
    And thats about it with the instantiating part. I have a UI button that calls a function when pressed, which does a bunch of calculations. And in this function, I have a helper function called GetEnemyList(shown in the screen shot) that does the following code:

    Code (CSharp):
    1.     m_enemies = GameObject.FindGameObjectsWithTag("Enemy");
    2.     m_enemyCount = m_enemies.Length;
    So I'm basically saving the array of game objects and the number of enemies in this array for future uses. I then call the initialization function for these enemies. Hope that was helpful!
     
  4. magnite

    magnite

    Joined:
    Dec 12, 2012
    Posts:
    125
    Okay, I get the feeling that you are calling the Instantiate method somewhere else with the enemyPrefab and not realising it. Try adding a Debug.Log at the beginning and end of your instantiation method.
    Code (csharp):
    1. Debug.Log(GameObject.FindGameObjectsWithTag("Enemy").Length + "");
    This will tell you if any GameObjects were spawned prior to the Instantiation call, and how many were spawned after. If there is something spawned before you called it, and there is only supposed to be one, then check your scripts for a rogue Instantiate call that you may have forgotten to take out. If the first print came out as it should, and the second doesn't then you have two approaches: Check your prefab for any child GameObjects that also have the tag of "Enemy" because FindGameObjectsWithTag also counts the children, not just the parent. You're other option is to report it as a bug and wait.
     
  5. lkarus

    lkarus

    Joined:
    Feb 25, 2015
    Posts:
    51
    So I placed the Debug.Log function when I instantiate objects. It works perfectly fine. I am getting the exact number of enemies on the map compared to the number that is being logged. I checked the prefab and only the parent has the tag "Enemy". Rest are untagged.

    Do you think its a viable, yet temporary solution to when I call the FindGameObjectsWithTag, loop through it and see if any objects has the word 'clone' in it, and if yes, remove it?
     
  6. magnite

    magnite

    Joined:
    Dec 12, 2012
    Posts:
    125
    Hmm, weird. Someone else may have more insight into the issue than me. That is a perfectly viable option. Just remember to always make sure to remove the clone from the name from any enemies you purposely spawn in the future. Would hate for you to start spawning stuff and then them not be recognized. ;)
     
    lkarus likes this.
  7. lkarus

    lkarus

    Joined:
    Feb 25, 2015
    Posts:
    51
    Seems like for now, I'll have to remove it from the array manually. Thank you for the help though!!
     
  8. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    So there is two mouse clicks, one for placing the enemy and the other one clicking UI button. Are you absolutely sure your code is not instantiating another enemy when clicking the ui button?
     
  9. lkarus

    lkarus

    Joined:
    Feb 25, 2015
    Posts:
    51
    Nope. I ctrl+f every instantiate keyword to see if that was the case, but no I didnt find anything weird.
     
  10. chelnok

    chelnok

    Joined:
    Jul 2, 2012
    Posts:
    680
    I did not mean that if you by mistake have instantiate in ui script, what i meant was perhaps the script responsible instantiating enemies, registers not only the first click (when you place the enemy) but also the second mouse click (when you are clicking ui button). Just in case you didnt know, you can do project wide search for keyword in monodevelop -> search -> find in files.
     
  11. lkarus

    lkarus

    Joined:
    Feb 25, 2015
    Posts:
    51
    When I mouse click on a grid, I shoot a raycast from the camera toward the plane I have. If the raycast results to a hit and the tag of the hit object is not the plane (currently I have "Enemy" and "Wall"), I will not place any objects on the plane at that cell. And even if an object is instantiated on my 2nd click (UI button), it should appear on my inpsector shouldnt it? I couldnt see any other game object except the Enemy that I have instantiated using the first mouse click.