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
1
Question by Luk3ling · Apr 13, 2015 at 12:51 AM · instantiateprefabchild

Instantiate prefab as child - Won't work, don't know why.

I've done this before, I even tore code out of a previous project that I know to work, but for some reason, it does not.. I get:

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

Regardless of what I've tried.

     void Update () 
     {
     if (Input.GetKey (KeyCode.Mouse1) && Time.time > fireRate)
         {
             fireRate = Time.time + 1.5f;
             Rigidbody2D bladeClone;
             bladeClone = Instantiate(blade, transform.position, transform.rotation) as Rigidbody2D;
             bladeClone.transform.parent = transform;
         }
     }
 }
 

I have no idea why it doesn't work in one of the multiple iterations i've tried.

I've stored the transform as a variable in both the object to which the script is attached AND in much more complicated fashion; through a script on the object that is being instantiated itself, among other things.

No variation works, neither GameObject.Find, Storing the transform as a local or even global variable.

If I press Mouse1 and pause after the object appears and MANUALLY parent it to the object in question, nothing bad happens, and I get the exact result I would've expected the code to produce.

I'm at a loss. Code that used to work in previous projects has also broken for seemingly no reason.

I've been beating my head against this for hours now.

What am I missing here?

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

· Add your reply
  • Sort: 
avatar image
0

Answer by Luk3ling · Apr 13, 2015 at 02:03 AM

I eventually tried a new approach via tagging and searching from the instantiated object by attaching the following script to the prefab:

 using UnityEngine;
 using System.Collections;
 
 public class basicBldTest : MonoBehaviour {
 
     Transform parent;
 
     void Start () {
         parent = GameObject.FindGameObjectWithTag                   ("MeleePivot").transform;
         this.gameObject.transform.parent = parent;
    }
 }

I shouldn't have to have done this, however, so the whole thing still irks me.

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

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

18 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

Related Questions

How do I get the global position 2 Answers

Check if child exists and instantiate as child 1 Answer

Instantiate the subchild (the renderer) from prefab? 1 Answer

Prefab - Child of GameObject. 0 Answers

prefab question 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