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 gourangas · Dec 16, 2014 at 07:50 PM · transformtransform.parent

transform.parent not working

 void OnCollisionEnter2D(Collision2D stay){
                 if (stay.gameObject.tag == "Player" && !onPlatform){
                      player.transform.parent = transform;
                     onPlatform = true;
                 }
         }

I need to parent my character to a moving platform when they collide, simple as that. onPlatform variable will become true, but the parent instruction will not be executed. Any help appreciated.

Comment
Add comment · Show 1
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 gourangas · Dec 16, 2014 at 08:53 PM 0
Share

"onPlatform variable will become true, but the parent instruction will not be executed" this means putting a debug log is useless cause i told you the if it works...and the name...rly i dont think that changing the varable name will make a miracle. Here is the entire code: using UnityEngine; using System.Collections;

     public class LefttoRightPlatform : $$anonymous$$onoBehaviour {
     
         int direction = 1;
         GameObject player;
         bool onPlatform = false;
     
         void Start(){
             player = GameObject.Find ("PlayerChar");
             player.transform.parent = null;
         }
     
         void Update () {
     
             player.transform.parent = null;
                     if (transform.position.x < -64)
                             direction = 1;
                     if (transform.position.x > -43)
                             direction = -1;
                     transform.Translate (Vector2.right * direction * 2f * Time.deltaTime);
     
             }
             void OnCollisionEnter2D(Collision2D stay){
                     if (!onPlatform){
                          player.transform.parent = gameObject.transform;
                         Debug.Log("shit");
                         onPlatform = true;
                     }
             }
     } 

3 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by hari2015 · Mar 18, 2018 at 02:03 PM

Try transform.parent.name

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
avatar image
0

Answer by MrAkroMenToS · Dec 16, 2014 at 08:38 PM

Use SetParent instead of what you are using. And make sure you set onPlatform false when exiting. On the other hand i recommend you to move this to the player's movement. and use this structure if you do so

 void OnCollisionEnter2D(Collision2D stay){
                  if (stay.gameObject.tag == "Platform" && !onPlatform){
                       //Here you should set the parent of transform to stay.gameObject.transform, you can use both transform.parent or SetParent, it's up to you

                  }
          }

Comment
Add comment · Show 3 · 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 Jeff-Kesselman · Dec 16, 2014 at 08:46 PM 0
Share

Transform.parent= aught to work. i use it all the time. I suspect something else is wrong with his code.

He needs to properly trace and figure out what assumption he is making is false.

avatar image MrAkroMenToS · Dec 16, 2014 at 08:53 PM 0
Share

I edited the answer i think there will be something with the player gameobject. It would be better to place this logical block to the player's script because it should be there.

But if you exit from the platform you sould unparent your player.

++If this code is already in the player's code it's logically not good because you set the parent to player. (player will be player's parent)

avatar image gourangas · Dec 16, 2014 at 08:56 PM 0
Share

Ok $$anonymous$$r.Akro i will try using SetParent()...and if it wont work ill move the script to the character

avatar image
0

Answer by MrAkroMenToS · Dec 16, 2014 at 09:06 PM

It is very simple you make your player.transform.parent null in every update. Make sure you only make it null if you exit from the platform

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

undoing parenting of object's transform 1 Answer

Transform.parent returns only null 1 Answer

parent.transform creates new game object 1 Answer

Picking up object with transform.parent does not work 1 Answer

How can I create multiple camera locations. 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