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. Dismiss Notice

Reading tags from prefabs loaded into the scene

Discussion in 'Scripting' started by Shadowing, Feb 12, 2015.

  1. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,627
    At the moment I have an issue where I need to find the closest game object with the tag "Player"
    These player game objects are prefabs
    I have a function below. When I Debug variable gos it returns UnityEngine.GameObject[]
    Which I assume that means empty.
    But yet the function returns a player game object still.

    Is it not possible to read tags from prefabs loaded into the scene?


    Code (csharp):
    1.  
    2.  
    3. GameObject FindClosestObjectByTag(string Tag) {
    4.  
    5.     GameObject[] gos;
    6.     gos = GameObject.FindGameObjectsWithTag(Tag);
    7.  
    8.     Debug.Log (gos); // this returns "UnityEngine.GameObject[]"
    9.  
    10.     GameObject closest = null;
    11.     float distance = Mathf.Infinity;
    12.     Vector3 position = transform.position;
    13.  
    14.     foreach (GameObject go in gos) {
    15.        Vector3 diff = go.transform.position - position;
    16.        float curDistance = diff.sqrMagnitude;
    17.  
    18.        if (curDistance < distance) {
    19.           closest = go;
    20.           distance = curDistance;
    21.        }
    22.     }
    23. return closest;
    24. }
    25.  
    26.  
     
  2. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    No. It means that the variable contains an array of GameObjects.
     
  3. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,627
    thanks for the response toreau
    When I do Debug.Log (gos.Length);
    It does say there are 2.

    Isn't there a way to show the array as in printing it in log?

    So I guess it is finding all Players then.
    The problem is its not finding the closest ones
    so i'll debug that then. Its quite interesting though cause this function was taken right out of the unith3d manual
     
  4. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,627
    This function won't return the closest either


    Code (csharp):
    1.  
    2.  
    3.  GameObject FindClosestObjectByTag(string Tag) {
    4.     GameObject[] gos;
    5.     gos = GameObject.FindGameObjectsWithTag(Tag);
    6.     GameObject closest = null;
    7.     foreach (GameObject go in gos) {
    8.     if(!closest){
    9.        closest = go;
    10.     }
    11.  
    12.     if(Vector3.Distance(transform.position, go.transform.position) <= Vector3.Distance(transform.position,       closest.transform.position))
    13.        closest = go;
    14.     }
    15.  return closest;
    16.  }
    17.  
    18.  
     
  5. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    I'm not sure how its even compiling but you seem to be missing a closing brace }
    for the foreach clause. This code will always return the first go in gos i.e the foreach
    is running only once. So you need to put a closing brace } before the return closest;
    line.
     
  6. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    Also, I would optimize this by
    caching the closest distance instead of doing a Vector3.Distance each loop like this :

    (Note : Below code only for the foreach loop)

    Code (CSharp):
    1. float closestDistance = 10000f; // really high value
    2. float dist;
    3. foreach (GameObject go in gos) {
    4.     if(!closest){
    5.        closest = go;
    6.     }
    7.     dist = Vector3.Distance(transform.position, go.transform.position);
    8.  
    9.     if(dist <= closestDistance)) {
    10.        closest = go;
    11.        closestDistance = dist;
    12.     }
    13.   } // The missing closing brace !
    14. return closest;
    15. }
     
  7. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,627
    thanks for the response Game boy
    so apparently all versions of the function work. Sitting here trying to problem solve this for over 6 hours.

    I realize my issue is that the enemy mobs are not updating their location to the other players.
    OnPhotonSerializeView is doing nothing right now. I have no idea why :(
    I even put Debug.Log("test"); inside of it and it never logs

    I use this function also when putting players in sync and it works but I also put Debug.Log("test") on that one to and it also doesn't log.

    So I don't know what I'm doing wrong here.


    Code (csharp):
    1.  
    2.  
    3. GameObject closestPlayer;
    4. Transform closestPlayerTransform;
    5.   PlayerHealth playerHealth;
    6.   EnemyHealth enemyHealth;
    7.   NavMeshAgent nav;
    8. Vector3 position;
    9. Quaternion rotation;
    10.  
    11.   void Update (){
    12.        if(PhotonNetwork.isMasterClient){
    13.  
    14.           closestPlayer = FindClosestObjectByTag ("Player");
    15.           Transform closestPlayerTransform = closestPlayer.transform.GetChild(0);
    16.           playerHealth = closestPlayer.GetComponent <PlayerHealth> ();
    17.  
    18.           if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0){
    19.              nav.SetDestination (closestPlayerTransform.position);
    20.           }else{
    21.              nav.enabled = false;
    22.           }
    23.        }
    24.   }
    25. void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
    26.     if(stream.isWriting){
    27.        stream.SendNext(transform.position);
    28.        stream.SendNext(transform.rotation);
    29.     }else{
    30.        position = (Vector3)stream.ReceiveNext();
    31.        rotation = (Quaternion)stream.ReceiveNext();
    32.     }
    33. }
    34.  
    35.  
     
    Last edited: Feb 12, 2015
  8. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,627
    If I drag the enemies transform into their own photoview then everything works except enemies are all jaggy looking

    so the question is how do I keep AI enemies in sync with all the other human players
    Should I even be trying to sync all the enemy AI continuously like that?
    I'm trying to make a game where you have up to 6 people in a group going around killing stuff.
    Just 6 people to a networking instance.
     
    Last edited: Feb 12, 2015