Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

gameobject name find... must I include (clone)?

Discussion in 'Scripting' started by San_Holo, Dec 18, 2014.

  1. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    Hi...

    I've got three different prefabs named differently and using their name I'm getting that and then doing something according to its name, I can see I'm getting the name of the object but it's not executing the code after that and perhaps its me, so have a gander, no errors, perhaps I'm missing something...

    Ok

    Code (csharp):
    1. GameObject.FindGameObjectWithTag ("Player").SendMessage ("PowerUp", gameObject.name);
    I get the name there via OnTriggerEnter on the object itself and then pass it to my 'powerup' function in another script on my player, example snippet below:

    Code (csharp):
    1. void PowerUp (string powerType)
    2.         {
    3.                 if (_mySceneManager.levelState == LevelState.NextLife)
    4.                         return;
    5.                 powerLevel++;
    6.                 print ("power type:" + powerType);
    7.                 if (powerType == "PowerUpDamage") {
    8.                         GameObject.FindGameObjectWithTag ("Player").SendMessage ("DecreaseDamageBar", 5, SendMessageOptions.DontRequireReceiver);
    9.                 }
    printing the name of the prefab to see shows the (clone) text which i assumed is ignored and it's just me and my brain...
    perhaps I could just tag my three power-up prefabs and just check those... hmmm any advice would be a boon, thanks
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    When using Find, you need to use the exact name, so if it has (Clone) in it, then include that.

    --Eric
     
    San_Holo likes this.
  3. Coldcalifornian

    Coldcalifornian

    Joined:
    Feb 11, 2014
    Posts:
    14
    First off, the (clone) text is not ignored. Whenever you Instantiate a GameObject and don't specify what the name of the instantiated GameObject is, the method will tack on the (clone) string.

    You could set the name of the instantiated GameObject right after you instantiate it
    Code (CSharp):
    1. GameObject clonedObject = (GameObject) Instantiate(yourPrefab,position,rotation);
    2.         clonedObject.name = yourPrefab.name;
    You will be able to find the GameObject by the original name at this point.
     
    way3edgy and San_Holo like this.
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Better yet, store the reference when you instantiate the object, so you don't need to use Find at all.

    --Eric
     
    way3edgy, Kiwasi and San_Holo like this.
  5. Coldcalifornian

    Coldcalifornian

    Joined:
    Feb 11, 2014
    Posts:
    14
    I agree with Eric. I get rid of any Find methods as fast as I can.