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
0
Question by nopogo · Feb 02, 2013 at 04:53 PM · camerainstantiatetransformprefabparent

Howto set main.camera as a parent when main.camera itself is a child of a prefab

So with below code, I am trying to make an instantiated object the child of main.camera so it looks as if the person activating it is "holding it"

 using UnityEngine;
 using System.Collections;
 
 public class Haxxor3000Behaviour : MonoBehaviour {
     private GameObject haxxorObject;
     private Transform cameraTransform;
 
 
     void SwitchState (bool setActive) {
         
         cameraTransform = Camera.main.transform;
         if(setActive){
             haxxorObject = Instantiate(this, cameraTransform.position + cameraTransform.forward * 2, cameraTransform.rotation) as GameObject;
             this.transform.parent = cameraTransform;
             
         } else{
             Destroy(this);
         }
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }



However this results in the following error:

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

I think this happens because main.camera is itself a child of the prefab player.

Is there any way to fix this? or maybe another solution to this problem I am not seeing?

All help is appreciated,

cheers Nopogo

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
Best Answer

Answer by Wolfram · Feb 02, 2013 at 05:05 PM

Assuming this script is part of your prefab, don't you mean

 haxxorObject.transform.parent = cameraTransform;

?

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 nopogo · Feb 03, 2013 at 08:54 AM 0
Share

I tried that also. But weirdly haxxorObject is null so that results in a NullReferenceException

which I should have mentioned since that also baffles me.

edit

After looking around some more I changed the instantiation to:

 private Transform haxxorTrans;
 
 
 haxxorTrans = (Transform) Object.Instantiate(this, cameraTransform.position + cameraTransform.forward * 2, cameraTransform.rotation);

But this resulted in a:

InvalidCastException: Cannot cast from source type to destination type.

I feel that takes me 1 step further towards the solution, but now I'm stuck again :P

avatar image Wolfram · Feb 03, 2013 at 03:36 PM 0
Share

Ah, I see. "this" is your script object, which is a monobehaviour. If you use "gameObject" ins$$anonymous$$d, Instantiate can be casted to GameObject. Or with your latest version, replace "this" with "transform".

Instantiate will always clone the whole GameObject (-hierarchy), no matter which subcomponent of the parent you feed to it. However, the object returned will be of the same type as the object you put in.

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

10 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

Related Questions

Parenting GameObjects 1 Answer

parenting instantiated object 2 Answers

Get transform of instantiated Prefab; Help appreciated. 1 Answer

Setting parent of instantiated object fails (Error: setting parent of prefab is disabled...) 1 Answer

Instantiate a Prefab as child 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