• 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
1
Question by AozakiKyuuji · Nov 29, 2013 at 01:54 PM · nullreferenceexceptionparentclonename

Is it possible to get a cloned gameObject's parent name?

Hey guys, right now, I can't seem to get my cloned object's parent's name.

After instantiating my object, it became a child of another object.

So, this code

 if (transform.parent.name == "DropZone1")
     pos = new Vector3(-0.475f, 0,0);

returns a NullReference. I'm guessing it's trying to reference its prefab, which doesn't have a parent. But it's already in the game, so it should be trying to reference it's current parent instead.

     print (transform.parent.name);
     Debug.Log (transform.parent.name);

both also returns a NullReference.

Anyone knows a solution to this? Thanks!

Comment
Add comment · Show 2
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 MrSteve1 · Nov 29, 2013 at 02:00 PM 0
Share

It may be a nesting error! Can you show the code that make the clone a child please? I have a theory!

avatar image AozakiKyuuji · Nov 29, 2013 at 02:17 PM 0
Share

It's a Drag & Drop from NGUI

 void OnDrop (GameObject go)
     {
         DragDropItem ddo = go.GetComponent<DragDropItem>();
             
         if (ddo != null)
         {
         GameObject child = NGUITools.AddChild(gameObject, ddo.prefab);
         
         Transform trans = child.transform;
         trans.position = UICamera.lastHit.point;
         Destroy(go);
         }
     }
 

NGUITools.cs

 static public GameObject AddChild (GameObject parent, GameObject prefab)
     {
         GameObject go = GameObject.Instantiate(prefab) as GameObject;
 
         if (go != null && parent != null)
         {
             Transform t = go.transform;
             t.parent = parent.transform;
             t.localPosition = Vector3.zero;
             t.localRotation = Quaternion.identity;
             t.localScale = Vector3.one;
             go.layer = parent.layer;
         }
         return go;
     }

1 Reply

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

Answer by raimon.massanet · Nov 29, 2013 at 02:33 PM

I am assuming the code that's giving you a NullReference is attached to the instantiated object. So, when are you executing that code?

Maybe you are executing it in Awake which is being executed before AddChild has parented the new object to the desired parent?

Comment
Add comment · Show 2 · 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 AozakiKyuuji · Nov 29, 2013 at 02:36 PM 0
Share

omg... you're right!

Because Awake is calling this method, so I didn't notice it and thought it's somewhere after Awake... Thanks man! and Steve for trying to help ^^

avatar image raimon.massanet · Nov 29, 2013 at 02:39 PM 0
Share

No problem, I'm glad I helped you.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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 avatar image avatar image avatar image avatar image

Related Questions

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

Unity Creating Invisible Clone (C#) 6 Answers

how to change the Parent of an object to the current object that a script is on? 1 Answer

Parenting an instantiated Prefab... again 1 Answer

Search for an object by name null reference exception 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