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

Transform.Find() not working with prefab

Discussion in 'Scripting' started by Jadecraft4, Jul 10, 2020.

  1. Jadecraft4

    Jadecraft4

    Joined:
    Mar 1, 2020
    Posts:
    27
    I've checked a while and I still can't figure this out. My goal is to give my enemy a prefab gun and when the game starts, the options like where the tip of the gun is will auto-fill. It always returns null so it doesn't auto-fill.
    Code (CSharp):
    1. Public GameObject Pistol;
    2. public Transform GunTip;
    3.  
    4. void Start()
    5. {
    6. Pistol = this.gameObject.transform.Find("Pistol").gameObject;
    7. if (Pistol != null)
    8. {
    9. GunTip = Pistol.transform.Find("Tip").transform;
    10. }
    11. }
    12. // other part of the script that works fine manually
    Hierarchy.PNG
    I've also tried Pistol = transform.Find().gameObject;
    The script is on the gameobject in blue.
    Any help is appreciated.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Where and when do you create the Pistol?
     
  3. Jadecraft4

    Jadecraft4

    Joined:
    Mar 1, 2020
    Posts:
    27
    Before the game starts. I just drag the prefab onto the enemy.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    And which object is this script attached to?
     
  5. Jadecraft4

    Jadecraft4

    Joined:
    Mar 1, 2020
    Posts:
    27
    SkelMesh_Bodyguard_04
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    So which thing is null? Pistol or GunTip?
     
  7. Jadecraft4

    Jadecraft4

    Joined:
    Mar 1, 2020
    Posts:
    27
    Did some testing. I fixed the pistol by putting it in update. My new problem is I'm getting errors saying the object doesn't exist. Which for some of the other enemies is true. How do I find if the object exist for each enemy?
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Well there's no reason really to be getting the GameObject just to get back to the transform:

    Code (CSharp):
    1. public Transform Pistol;
    2. public Transform GunTip;
    3.  
    4. void Start()
    5. {
    6.   Pistol = transform.Find("Pistol");
    7.   if (Pistol != null)
    8.   {
    9.     GunTip = Pistol.transform.Find("Tip");
    10.   }
    11. }
     
  9. Jadecraft4

    Jadecraft4

    Joined:
    Mar 1, 2020
    Posts:
    27
    I did some more testing and I think start only does the first 3 lines of code. I moved my gun code to the top and it all works, but the code after it doesn't.
    Code (CSharp):
    1. void Start()
    2. {
    3. Pistol = transform.Find("Pistol");
    4. AK = transform.Find("AK-47");
    5. //Line 3 works
    6. //Line 4 does not work
    7. }
     
  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Do you have any errors in your console?
     
  11. Jadecraft4

    Jadecraft4

    Joined:
    Mar 1, 2020
    Posts:
    27
    Yes, the fourth line is finding my Nav Mesh Agent and since it's not finding it, I get a lot of errors. (New conclusion: I can't use .Find() twice. only one line will work. Edited)
     
    Last edited: Jul 10, 2020