• 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 cinemator · Mar 24, 2020 at 02:21 AM · transform.parent

transform.parent for individual object

Hello, I have a prefab that spawning in the scene with a rigid body sleeper script on the child. But When I run this code it affects all my prefabs in the scene. I want to affect only its local parent. any suggestions?

 using System.Collections;
 using UnityEngine;
 
 public class RigidSleeper : MonoBehaviour
 {
     private Rigidbody rbGO;
     
     public float timer = 5 ;
     bool setgo = true;
     public float Yforce = 1;
 
 
     private void Start()
     {
         rbGO = transform.parent.parent.parent.GetComponent<Rigidbody>();
     }
 
 
     void FixedUpdate()
     {
         
 
         if (Input.GetMouseButtonDown(0))
             setgo = false;
 
        
 
 
 
         if (setgo==true)
         {
 
             
 
             rbGO.AddForce(0, Yforce, 0);
 
 
     }
         else if (setgo==false)
         {
             rbGO = transform.parent.parent.parent.GetComponent<Rigidbody>();
             rbGO.Sleep();
             
 
             if (Input.GetMouseButtonUp(0))
             StartCoroutine(coroutine());
 
 
 
         }
 
 
 
         IEnumerator coroutine()
         {
             if(setgo==false)
             {
                 yield return new WaitForSeconds(timer);
                 setgo = true;
             }
 
 
         }
 }
 }
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
0

Answer by cinemator · Mar 24, 2020 at 10:11 AM

I wrote the code like that because my objects is the actually grandson of its parent.

Where do I have to look for to act all prefabs differently from their childs

Comment
Add comment · Show 1 · 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 streeetwalker · Mar 24, 2020 at 12:42 PM 0
Share

@cinemator, Setting properties on one instance of a prefab or any of it's children won't affect another instance of the same prefab - the only way that happens is if you are either modify the prefab itself and not an instance; Or If you have instantiated two prefabs and then made them children of some existing object. Then, for example, applying motion to that new parent will make both prefab instances move.

We cannot tell in your script what is going on without seeing what your set up and other scripts look like.

OK, You clarify that you're acting on a grandson, but code looks like your climbing back up to a great-grandparent.

I think:

transform.child.grandchild.great-grandchild

and in reverse:

transform.parent.grandparent.great-grandparent.

so it looks like either you're climbing back too far, or you aren't describing the relationship to match your code.

(A prefab is essentially a template. I don't know, but maybe if you try to climb too far back out of a prefab instance, you do actually modify the prefab template? That's a theory that should be tested...!)

avatar image
0

Answer by streeetwalker · Mar 24, 2020 at 07:20 AM

@cinemator, Well, your situation is more complex than you let on! If all you had was one parent and a child, then why does your code do:

    transform.parent.parent.parent.GetComponent<Rigidbody>()  ?

It looks like you are climbing the hierarchy all the way back up to some great-grandparent. If all you prefabs share a common parent, there is no "local" parent.

Perhaps what you mean to do is just:

   transform.parent.GetComponent<Rigidbody>()  ?
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.

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

200 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 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 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 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 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 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 can I parent a GameObject to and instance of another GameObject? 1 Answer

Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption - Help Needed Urgently 1 Answer

Velocity of parent local to child 1 Answer

How to set cursor as a parent 0 Answers

setParent. what am i doing wrong? 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