Search Unity

pathname and string add

Discussion in 'Scripting' started by Mike, Jul 31, 2007.

  1. Mike

    Mike

    Joined:
    Dec 1, 2006
    Posts:
    118
    Hi!

    I want to access a Gameobject which is a child of Gameobject "gui". The name of the GO comes from a trigger collision.

    But all I get is »NullReferenceException«...

    Here's what I got...


    Code (csharp):
    1.  
    2. function OnTriggerEnter (col : Collider) {
    3.    
    4.      infoBox=GameObject.Find("gui/"+col.name);
    5. }
     
  2. Omar Rojo

    Omar Rojo

    Joined:
    Jan 4, 2007
    Posts:
    494
    To get the gameObject of the collider use

    Code (csharp):
    1. infoBox = col.gameObject;
    If you still want to use that option, put

    Code (csharp):
    1. Debug.Log ("gui/" + col.name);
    to see if its really generating the hierarchy name correctly.

    .ORG
     
  3. Mike

    Mike

    Joined:
    Dec 1, 2006
    Posts:
    118
    Thanks!

    The trigger collection works - but what I need now is more complex:

    When my FPC hits several trigger colliders (let's say the first one is called "test1") a GUI texture that is also called "test1" should be set to active=true.

    And here I have that problem: I need "col.name" to get the name of the collider (="test1") - and then change the status of GUI texture "test1".

    Problem no 1:

    How can I access the GUI texture when it's within a hierarchy?

    And how do I add that path to collider.name? Is it "+" ?
     
  4. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    The same as you do other Game Objects:
    Code (csharp):
    1. // like this...
    2. var tGuiTexture = GameObject.Find("/Foo/Blah/test1");
    3.  
    4. // or like this...
    5. var tGuiTexture = GameObject.Find("test1");

    Yes, "+" is what you use to concatenate strings together and the path you pass to the Find() function is provided as a string.
    Code (csharp):
    1. // imagine having three variables...
    2. var tFirstToken = "/Foo";
    3. var tSecondToken = "/Blah";
    4. var tThirdToken = "/test1";
    5.  
    6. // then concatenate those together when calling Find()...
    7. var tSomeGO = GameObject.Find(tFirstToken + tSecondToken + tThirdToken);