- Home /
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?
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
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.
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
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.
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
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;