Search Unity

GameObject Member Problem

Discussion in 'Scripting' started by yangmeng, Sep 23, 2007.

  1. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    In the code below, both obj1 and obj2 are being defined as game objects. Transform works fine with obj1, but for obj2 (which is being found by its tag) the editor objects, saying "'transform' is not a member of'(UnityEngine.GameObject)'".
    Could someone tell me what's wrong and or how to work around this limitation?

    Code (csharp):
    1. var obj1 : GameObject;
    2.  
    3. function OnMouseDown () {
    4.     obj1.transform.position.y -= 1;
    5.     var obj2 = GameObject.FindGameObjectsWithTag ("blocks");
    6.     obj2.transform.position.y -= 1;
    7. }
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The problem is that FindGameObjectsWithTag returns an array of objects instead of one object. If you actually just want one, then use FindWithTag instead, which returns a singular object. Otherwise loop through the array.

    --Eric
     
  3. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    FindGameObjectsWithTag returns an array of GameObjects. That's why you get the error.
    Code (csharp):
    1. var obj1 : GameObject;
    2.  
    3. function OnMouseDown () {
    4.    obj1.transform.position.y -= 1;
    5.    var obj2 = GameObject.FindWithTag ("blocks");
    6.    if(obj != null)
    7.      obj2.transform.position.y -= 1;
    8. }
     
  4. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    Dang. I had a similar issue some time ago. I should have recognized it as a related problem.
    Thanks for your help!
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Feature/adjustment request: would it be possible to adjust the error display to say, instead of (GameObject), something more like GameObject[] or (array of GameObject)? (GameObject) in no way suggests "array" to me, and apparently i'm not the only one....
     
  6. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    Hey, I have been using the tip above but I have found out that that this goes through each of the objects "door" one at a time. I want them all to be acted upon by the script at the same time. Is there a good way of doing this?

    Code (csharp):
    1. var Doors = GameObject.FindGameObjectsWithTag ("door");
    2.  
    3. for (d in Doors) {
    4.     audio.Play ();
    5.     for (t=0;t<10;t++) {
    6.         yield WaitForSeconds(.01);
    7.         d.transform.position.y -= .1;
    8.         }
    9. }
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. var Doors = GameObject.FindGameObjectsWithTag ("door");
    2. audio.Play ();
    3. for (t=0;t<10;t++) {
    4.     yield WaitForSeconds(.01);
    5.     for (d in Doors) {
    6.         d.transform.position.y -= .1;
    7.     }
    8. }
    Unless I misunderstood? It's not possible for the computer to move a lot of objects simultaneously (well, if you had a massively parallel CPU array I guess you could). So you always have to loop through one at a time; the trick is to do it fast enough so that it seems like they're all moving at once. :) If you find a function that seems to deal with multiple objects, it's still doing everything one-at-a-time in loops "behind the scenes".

    --Eric
     
  8. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    Worked perfectly. Thanks! And thanks for explaining how and why it works like that, too.