Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Jan 17, 2016 at 02:57 PM by Denkaa for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Denkaa · Jan 10, 2016 at 04:08 PM · instantiateparentchild

Instantiate an object as child

Hello,

I'm trying to make an overlay and i want to instantiate the hearts in the "Overlay" GameObject. I searched on Internet (i actually tried things found in the first two pages of google) but nothing works.

Here is my code :

 using UnityEngine;
 using System.Collections;
 
 public class Overlay : MonoBehaviour {
     public Transform CoeurPlein;
     public Transform CoeurVide;
 
     // Use this for initialization
     void Start () {
     
 
         GameObject TCoeurPlein = Instantiate (CoeurPlein, new Vector3 (0,0, 0), Quaternion.identity) as GameObject;
         TCoeurPlein.transform.parent = GameObject.Find("Overlay").transform;
 
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }
 

And the error message :

NullReferenceException: Object reference not set to an instance of an object Overlay.Start () (at Assets/script/Overlay.cs:13)

Thanks

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0

Answer by corn · Jan 10, 2016 at 04:30 PM

The error is there :

 TCoeurPlein.transform.parent = GameObject.Find("Overlay").transform;

The fact that there is a NullReferenceException on that line means that GameObject.Find("Overlay") returned null, so you're trying to access the transform of a null object. It appears that there isn't an "Overlay" GameObject in your scene.

When using methods the likes of Find, it's generally a good idea to check if it actually worked.

     void Start()
     {
         GameObject TCoeurPlein = Instantiate(CoeurPlein, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
         GameObject overlayParent = GameObject.Find("Overlay");
 
         if (overlayParent != null)
         {
             TCoeurPlein.transform.SetParent(overlayParent.transform);
         }
     } 


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 Denkaa · Jan 10, 2016 at 04:55 PM 0
Share

Thanks for the fast answer :)

I have this : alt text

I tried to find with tag, but that doesn't work . I tried to find with Find("$$anonymous$$ain Camera/Overlay") that doesn't work either, finding a new gameObject either (with name and tag).

zretdhetrt.png (4.1 kB)
avatar image corn Denkaa · Jan 10, 2016 at 05:52 PM 0
Share

GamObject.Find("Overlay"); works fine for me. $$anonymous$$ake sure you didn't make a typo, maybe your object is actually named "Overlay " (ending with a space), that'd be an easy mistake to overlook.

avatar image Denkaa corn · Jan 10, 2016 at 06:27 PM 0
Share

Evrything is ok with the overlay. I tried with the "if != null "and it got the print i put in it. So the probleme come from the instantiate object (TCoeurPlein). I tried it with the !=null and it doesn't get the print. Do you have any idea about this one?

Actually it looks like this :

 GameObject TCoeurPlein = Instantiate (CoeurPlein, new Vector3 (0,0, 0), Quaternion.identity) as GameObject;
 
         GameObject overlayParent = GameObject.Find ("Overlay");
         if (overlayParent != null) {
             TCoeurPlein.transform.SetParent(overlayParent.transform);
         }

Follow this Question

Answers Answers and Comments

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

Related Questions

Instantiating as a child of an object in hierarchy 1 Answer

Child with Rigidbody doesn't move with Parent with Rigidbody 3 Answers

Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, with the worldPositionStays argument set to false. 1 Answer

Cannot set parent for instantiated GameObject. (Exception: Can't destroy Transform component of...) 0 Answers

Child skews when parenting 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