• 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
Question by rheadedstepchyld · Nov 15, 2013 at 02:25 AM · errortransformprefabinstatiate

Setting the transform of an instantiated game object

Hello! I am new to posting on Unity Answers, though I have been stalking the threads for answers to my problems for quite sometime now... ;)

I am having an issue setting the transform of a game object that I have already instantiated. Now I've seen several threads already where people have similar issues, but they are all trying to change the transform of the prefab. I have instantiated the prefab into a variable and I am trying to change the transform of that variable. Here is the bit of code that I am talking about.

 var single : GameObject;
 private var temp:GameObject;
 
 temp = GameObject.Instantiate(single, location, single.transform.rotation) as GameObject;
 temp.transform.parent = transform;

The really frustrating part is that I'm doing this somewhere else in my code (what I believe to be the exact same thing) and it works. This one however....does not.

Also, the exact error I am getting:

"Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.

UnityEngine.Transform:set_parent(Transform)" Thanks a lot for any help!
Comment
AnnaS
oneness19
InterZept

People who like this

3 Show 7
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 Huacanacha · Nov 15, 2013 at 03:26 AM 0
Share

I think you're not supposed to overwrite transforms directly. Grab the position and rotation and set those:

 temp = GameObject.Instantiate(single, location, single.transform.rotation) as GameObject;
 temp.transform.parent.position = transform.position;
 temp.transform.parent.rotation = transform.rotation;

I guess there is some extra issues setting the transform directly with prefabs as they all come from the same template object.

avatar image rheadedstepchyld · Nov 15, 2013 at 04:21 AM 0
Share

Well, as I said above, I have code elsewhere that looks identical to this and it works, so I don't know why it wouldn't work here. I did however try your suggestion and now I'm getting a NullReferenceException.

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

UnitManager.BuildUnit (System.Object unit, System.Object location) (at Assets/Scripts/Player Scripts/UnitManager.js:77) This makes me think that my instantiation is returning a null, but I've checked each of the parameters and 'single' is a game object and 'location' is a position.
avatar image Huacanacha · Nov 15, 2013 at 05:42 AM 0
Share

Hmm I didn't change the "temp =" line from your original code, is that where the error is thrown? If not is temp null after the call to Instantiate?

And you said you checked that single, location and single.transform are all objects (not null)?

avatar image rheadedstepchyld · Nov 15, 2013 at 06:15 AM 0
Share

I didn't change it either. Every time I have gotten an error, it has always been on the 'temp.transform...' line.

Yeah, I just double checked. Single, location, and single.transform.rotation are not null.

avatar image Huacanacha · Nov 15, 2013 at 08:21 AM 0
Share

Ah of course the parent transform is null because it's not set to anything yet. Having read the transform.parent docs again it looks like what you are doing should work. Clearly it's some issue related to the prefab but I'm not sure what.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by luiscmendez · Nov 15, 2013 at 03:13 AM

Just giving it a once over it seems like this might be the type of line that you're looking for.

     temp = GameObject.Instantiate (single, transform.position, transform.rotation) as GameObject;

But is "location" in your above code another variable that you haven't written down above next to single and temp? I'm not really sure why that's there. :P

Comment

People who like this

0 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 rheadedstepchyld · Nov 15, 2013 at 03:25 AM 0
Share

Yes, sorry. This object is replacing another object. Location is the transform.position of the old object. Single.transform.rotation is used to make sure this object is rotated to the same angle that the original one is rotated to.

avatar image luiscmendez · Jul 13, 2016 at 07:30 PM 0
Share

I was messing around with something like this earlier and figured out how to do this when we get the error

Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.

We have to essentially create another instance of the object and assign it at runtime once the prefab has been instantiated. So something like this (C#)

 public Transform prefabParent;
     
 public GameObject myPrefab;
 private GameObject runtimeGO;
      
 void OnEnable ()
 {
      //Instantiate the prefab and assign it to the runtime GameObject
      runtimeGO = (GameObject) Instantiate(myPrefab);
      
      //Assign the position of the transform for the runtime GameObject
      runtimeGO.transform.position = gameObject.transform.position;
 
      //Assign the parent of the runtime GameObject
      runtimeGO.transform.parent = prefabParent.transform;
 }

Was that kind of what you were looking for?

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

How can I make this GameObjects array remain permanent when i activate? 1 Answer

Distance betwen two objects.Error,bug,etc. 2 Answers

still trying to access a destroyed transform... 2 Answers

Instantiating many objects 1 Answer

UnityException: Transform Child Out of Bounds 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