Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • 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 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
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...!)

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

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

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

what object does the parent child code go on? 1 Answer

Drag, Drop, and Attach 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