Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by S3dition · May 21, 2013 at 12:08 AM · transformhierarchychildrenfind

transform.Find always returns null

I created a script to fire projectiles from the barrel of a weapon. Everything works fine until I try to get the grandchild that is the point where the projectile is created (weaponHardpointVector[i] = transform.Find("EnemyTankTurret/EnemyWeapon1");:

         if(equippedWeapons[0] != null){
             
                 while(i < equippedWeapons.Length){
             
                 //Debug.Log ("Number of Equipped Weapons: " + equippedWeapons.Length);
             GameObject weapon = Instantiate(equippedWeapons[i], transform.position, transform.rotation) as GameObject;
                 //Debug.Log("Enemy Weapon Created: " + weapon.name);
             weapon.transform.parent = transform;
             weaponBay[i] = weapon.GetComponent<WeaponStats>();
                 Debug.Log("Weapon Bay 1: " + weapon.GetComponent<WeaponStats>());
             weaponHardpointVector[i] = transform.Find("EnemyTankTurret/EnemyWeapon1");
                 Debug.Log ("I Found : " + transform.Find ("EnemyTankTurret/EnemyWeapon1"));
                 Debug.Log ("Barrel Vector: " + weaponHardpointVector[i]);
                 i++;
                 b++;
                 
             
             }
         }

I keep getting:

NullReferenceException: Object reference not set to an instance of an object

Enemy+c__Iterator3.MoveNext () (at Assets/Scripts/Enemy.cs:103) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) Enemy:Update() (at Assets/Scripts/Enemy.cs:91) I've checked the hierarchy and that all the spelling is correct. I've tried moving the object up to be a child instead of a grandchild and it still returns null. Am I using transform.Find() incorrectly?
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · May 21, 2013 at 12:17 AM 0
Share

As far as I know, Find doesn't work with the hierarchy, so it would only be ËnemyWeapon1". But I might be wrong XD

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by dchen05 · May 21, 2013 at 12:18 AM

Its tough to say without seeing your hierarchy and on which object the script is located. transform.Find() looks in everything thats part of that transform that the script is on. So if your script is on EnemyTankTurret, you would only need to put

 transform.Find("EnemyWeapon1")


Also remove the slash from the name of EnemyTankTurret, slashes in gameobject names makes Unity confused, as it looks for them to be indicators of parent children relationships, like folders. http://docs.unity3d.com/Documentation/ScriptReference/Transform.Find.html

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image S3dition · May 21, 2013 at 12:20 AM 0
Share

The script is on EnemyTurret (the parent object). Let me get a screenshot of the hierarchy. turrethierarchy

turrethierarchy.png (2.4 kB)
avatar image dchen05 · May 21, 2013 at 12:25 AM 0
Share

Try removing the slash from the name of EnemyTankTurret, slashes in gameobject names makes unity confused. http://docs.unity3d.com/Documentation/ScriptReference/Transform.Find.html

avatar image S3dition · May 21, 2013 at 12:32 AM 0
Share

Interesting. I had tried it before without the "/" and it wasn't working. I guess something else was broken and I broke it more trying to fix it :/. Anyway, if you copy the above post as the answer I'll mark it.

Thanks for the help!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

transform.childCount == 0, but a child shows up in the hierarchy. How is this possible? 2 Answers

How do i find a bone on my character within a script? 2 Answers

Accessing children of instances vs children of original prefab 1 Answer

Make into child of a parent in c# 1 Answer

transform find with tags always return null 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges