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 /
avatar image
1
Question by Der_Kevin · Jan 26, 2016 at 02:55 PM · gameobjectparentcomponentfind

Find Script in parent Gameobject?

Heyo! so i got this Hierarchy:

 -Something
   -Puppet
     --Hips(Player)
   -CharContr
     -- AnimContr

the Player is already a public Gameobject, it gets set via on enter trigger

 void OnTriggerEnter (Collider myTrigger) {
         if(myTrigger.gameObject.tag == "Player"){
             Debug.Log("Box went through!");
             Player = myTrigger.gameObject;

from this Player Gameobject i want to turn off a script which is located in the "CharContr" and is called "UserControlThirdPerson"

how can i do this?

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

3 Replies

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

Answer by JoshuaMcKenzie · Jan 26, 2016 at 05:14 PM

Typically I have all control scripts rest on the root of the prefab. This makes it easy for any component in the prefab's hierarchy to find each other as I typically have the controlling script keep track of all components it may need to use or pass off.

An example heirarchy:

     Player (UserControlThirdPerson script here, keeps references to child objects)
      |- Mesh (has Animator and a Body collider)
      |- Selection Collider (catches click events so the player can be "selected")
      |- Range Collider (Trigger for Enemys Detection or attack range)
      |- *special*Origins (any special offsets I want easy access to)
       - World Space UI Canvas (any world space canvas like HP bars that follow the enemy)
     

I sometimes have multiple Colliders in a heirachy (three in this example: Body,Selection,Range/Detection) each having its own specific collision layer and a relay script (selection relay.cs, detection relay.cs) that passes trigger events upto the controller when needed. doing so gives me more control and enables better performance (for example Detection collider shouldn't hit selection Colliders, thus never will due to settings made in the edit>project settings>phyics, but can collide with body colliders).

As such I can get away with not even checking the tag, since most of the collision checking is done automatically for me by the Physics engine before it even hits my code.

Now you can simply do:

 void OnTriggerEnter(Collider other)
 {
     UserControlThirdPerson script= other.GetComponentInParent<UserControlThirdPerson>();
 
     if(script== null)
         // hit object is not valid or not the specific type of BodyCollider we're looking for
         return; 

     Player = script.gameObject; //or other.gameObject;

     //if, for some reason, you happen to need the RangeCollider your UserControlThirdPerson  script 
     //  can already have that reference stored for easy access
     Collider rangeCollider = script.RangeCollider;
 }

if your hierarchy is meant to be dynamically built at runtime(which it sounds like) still have some sort of controller/addresser script on the root and when a game object/component is added they message the controller via GetComponentInParent() and register themselves. This way the controller is able to serve as a local addressbook for all important components in its heirarchy

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 DiegoSLTS · Jan 26, 2016 at 03:06 PM

What object has that code?

There are lots of ways to get a reference to a component, but the most user friendly one is setting a public variable of the type of the script you want in the script you need it, and then drag and drop a gameobject with a component of the variable type into the field on the inspector.

Another method could be using the FindObjectOfType function if you know there's only one object with a component of that type.

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 Der_Kevin · Jan 26, 2016 at 03:17 PM 0
Share

What object has that code? its another object outside of the "something" so:

 -Anotherone
  -ObjectwithScript
 
 -Something
    -Puppet
      --Hips(Player)
    -CharContr
      -- AnimContr

but the most user friendly one is setting a public variable of the type of the script you want in the script you need it, and then drag and drop a gameobject with a component of the variable type into the field on the inspector.

that doesent work since the "Player" can always be a other gemobject and gets set when it is entering a Trigger

avatar image DiegoSLTS Der_Kevin · Jan 26, 2016 at 03:41 PM 0
Share

No, I meant you add a public variable in that script of type UserControlThirdPerson, then drag and drop the CharContr game object into the inspector field. Then if the player needs that reference you already have one to pass by.

avatar image Der_Kevin DiegoSLTS · Jan 26, 2016 at 04:10 PM 0
Share

but there can be more than one Gameobject with CharContr in the scene. and i just want to disable the CharContr for the Character that entered the Trigger

Show more comments
avatar image
0

Answer by giorashc · Jan 26, 2016 at 06:25 PM

You can get the parent object and use it to find the CharContr object and then the required script:

 Debug.Log("Box went through!");
 Player = myTrigger.gameObject;
 GameObject charContr = Player.transform.parent.parent.Find("CharContr").gameObject; 
 UserControlThirdPerson theScript = charContr.getComponent<UserControlThirdPerson>();
 theScript.enabled = false;
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

39 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

Related Questions

Find all gameObjects with same tag 1 Answer

Search for specific entity in an array? 1 Answer

Help to get access to the parent 2 Answers

Can I limit a .Find to the Parents and Child and not the Scene? 3 Answers

Find GameObject of tag "x" no matter where it is? 2 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