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 /
avatar image
0
Question by BULK_BICEPS · Dec 11, 2015 at 11:42 PM · parentprefab-instanceparent transform

[SOLVED] How do I set the parent of an instance of a prefab that was placed into gameworld, not instantiated in a script?

I'm trying to set the parent of a gameobject that I have placed in the world. The gameobject is an instance of a prefab. When I run the code, it throws the error

Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. UnityEngine.Transform:set_parent(Transform)

This is the code snippit in question. It is inside a script attached to the prefab of the object I'm trying to change the parent of.

 ...
 public GameObject handGrabContainer;
 ...
 public void beingGrabbed(){
     ...
     this.transform.parent = handGrabContainer.transform;
     ...
 }

I have searched online for a solution to this error, and there are several threads about it here, here and here for example. However, in all of those examples, the object is being instantiated in the script. Like this

 GameObject myThing= Instantiate(thing, Vector3 (0, 0, 0)) as GameObject;
  myThing.transform.parent = transform;

But I have an object that was placed into the gameworld in the Unity editor. How can I access the transform of the actual object, instead of the prefab, if I am not creating the object a script?

EDIT: It seems that problem was actually with the "handGrabContainer" object. I had it linked to the script in the script's prefab. The error that I was getting was from the handGrabContainer, not the this.transform.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by jmonasterio · Dec 11, 2015 at 11:56 PM

I'd suggest: Put a breakpoint on the bad line. And stop there in Visual Studio debugger.

Hover over the "this" (or add it to Watch window) in VisualStudio. Take a look at it. I bet "this" is not what you think it is.

Comment
Add comment · Show 5 · 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 BULK_BICEPS · Dec 12, 2015 at 12:14 AM 0
Share

Hello, thanks for your response. I did what you said, but I used $$anonymous$$onoDevelop ins$$anonymous$$d. It appears that "this" is an instance of the prefab. Alright, so what do I have to do to access the specific object, not the prefab?

avatar image jmonasterio BULK_BICEPS · Dec 12, 2015 at 12:18 AM 0
Share

Where is the code where you are calling beingGrabbed()? There you've got some object, and perhaps that's where to look.

You may be calling beingGrabbed on your Prefab, ins$$anonymous$$d of an object you Instantiated(). Hard to tell without seeing code.

avatar image BULK_BICEPS jmonasterio · Dec 12, 2015 at 12:27 AM 0
Share

As I said, I'm not using "Instantiate()" to create the object. I'm placing it in the world in the Unity Editor. The script is on the prefab of the object.

Show more comments
Show more comments
avatar image
1

Answer by monfera · Aug 14, 2016 at 11:54 PM

Just supply the fourth argument for Instantiate, which is the parent. No need for an extra line for this.

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

34 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

Related Questions

how add child to multi gameobject 0 Answers

Following object (arrow) slides off of a object 1 Answer

Parent-Child axial reference problem 0 Answers

Use Raycast to select empty parent game object? 1 Answer

How to move an object left and righ (looping) when it still move to left with it parent 0 Answers


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