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 RaulG · Jun 02, 2013 at 05:04 AM · objectparentpivot

Parenting MOVING objects during RUNTIME C#

 public bool platformMaxed = false;
     public static int platformSpawned;
     public GameObject PlatformPre;
     public GameObject ParentObject;
     
     public Transform platformChild;
 
     
     void Start () 
     {
         StartCoroutine(PlatformSpawning());
     }
     
 
     public IEnumerator PlatformSpawning()
     {
         while (platformMaxed ==false)
         {
             for (platformSpawned = 0 ;platformSpawned <= 3; platformSpawned++)
             {
 
                 Instantiate(PlatformPre, 
                 new Vector3(Random.Range(-15,15),Random.Range(1,15),0),
                 Quaternion.identity);
                 Debug.Log("Platform Spawned" + platformSpawned);
                 
                 if (platformSpawned >= 3)
                     {
                         platformMaxed = true;
                         break;
                     }
             }
             
             if (platformMaxed == true)
                 yield break;
             
             yield return 0;
             
             
         }
             
     }
     
     public void SpawnSinglePlatform()
     {
         Instantiate (PlatformPre, 
         new Vector3(Random.Range(-15,15),Random.Range(1,15),0),
         Quaternion.identity);
         Debug.Log("Platform Spawned" + platformSpawned);
     }
     
     
     void Update () 
     {
     }




What I need is, when the object is instantiated it gets parented to a Rotating Sphere object.

I can NOT use: Transform platformChild; platformChild = Instantiate(PlatformPre, New Vector3...) as Transform; As that will give me the following error: Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.

Alternatively, how would I go about rotating an object based on the pivot of another object?

Comment
Add comment · Show 2
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 Owen-Reynolds · Jun 02, 2013 at 03:43 PM 0
Share

Show the code giving the error?

avatar image RaulG · Jun 02, 2013 at 05:39 PM 0
Share

Ah damn, fixed it! Just had to make the ParentObject and PlatformPre Objects Transform ins$$anonymous$$d of Gameobject. Doesn't solve the Setting the Parent of a transform error though...

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Owen-Reynolds · Jun 02, 2013 at 05:20 AM

The error says you're trying to change the raw prefab, the blueprint object. In your case, something like PlatformPre.parent=. You can change the newly created gameObject all you want. Once it's spawned, it's the same as any other gameObject.

Follow your line Transform platformChild; platformChild = Instantiate(PlatformPre, New Vector3...) as Transform; with platformChild.parent=. You should be able to set platformChild.rotation to whatever you like, as well.

Comment
Add comment · Show 4 · 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 RaulG · Jun 02, 2013 at 05:31 AM 0
Share

Yeah, that's what I thought so too. But it either gives me a NullReferenceException at platformChild.parent = ;

Or it will still give me the Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.

avatar image Loius · Jun 02, 2013 at 06:01 PM 0
Share

What you are currently doing is something very similar to this:

 class Thing extends $$anonymous$$onoBehaviour {
   public GameObject instantiatable;
   public Transform parentToThis;
 
   public void Start() {
     ... GameObject.Instantiate(instantiatable);
 
     instantiatable.parent = parentToThis;
   }
 }

You need to do this:

   public void Start() {
     Transform temp = GameObject.Instantiate(instantiatable).transform;
 
     temp.parent = parentToThis;
   }
avatar image RaulG · Jun 02, 2013 at 06:13 PM 0
Share

Umm.

Transform temp = Transform.Instantiate(instantiatable) as Transform;

Otherwise it will give you the "Can't convert GameObject to Transform" and "Are you missing a Cast?" Errors.

Just getting a small NullReferenceError when I try to respawn the platforms, gimme a $$anonymous$$ to fix it. Then check to see if that helped!

avatar image RaulG · Jun 02, 2013 at 07:36 PM 0
Share

Okay Loius's method seems to have worked. Just need it to NOT spawn on top of each other.

Otherwise SWEET!

Transform temp = Transform.Instantiate(instantiatable) as Transform; <--- That version by the way.

OH and I also had to change EVERY BLOODY thing into Transform ins$$anonymous$$d of GameObject.

Also, the Instantiate had to be called inside the collision script otherwise you get a NullReferenceExcemption error.

AND I JUST REALIZED I A$$anonymous$$ THE BIGGEST IDIOT IN THE WORLD!!!!!!

So. I have this prefab... the one the platforms are parented to.. yeah... umm.. that is in fact a prefab.. and for some odd reason my brain did not read that as "PREFAB" and ins$$anonymous$$d read it as "DERP".

So problem solved!

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

16 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

Related Questions

How to be sure that the parent's position is the same is the child's position? 0 Answers

How to rotate around the parent in 2D? 1 Answer

problem with pivot point in maya 0 Answers

getting the object that the running script is attached to 1 Answer

Can't add Transform to an Array on Prefab. 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