- Home /
Getting a Trigger's parent
I have few planet spheres, with a big trigger called the Atmosphere as its child in each of them.
Players can hover around the space for unaffected by the planet's gravity unless they enter a "targetPlanet".
I wanted players that enters the Atmosphere trigger to consider the parent planet as their "targetPlanet".
I tried doing this, but the "targetPlanet" variable did not change at all, even though there is no errors.
function OnTriggerEnter (other : Collider) {
if (other.tag == "Player") {
motionCon = other.GetComponent("Planetary_Jumper");
motionCon.targetPlanet = transform.parent;
}
}
Helps?
^ That will refer me to the player's parent.
Wanted the "Trigger"'s Parent
Debug time! Throw in a Debug.Log statement within the if statement there, that tells you what transform.parent is, as well as what motionCon.targetPlanet is (before and after it is changed).
You should find these are the same, chances are your problem is that transform.parent is null, or transform.parent is the same as targetPlanet before it is even changed.
Answer by CodeElemental · Apr 11, 2014 at 02:27 PM
Considering this hierarchy :
PlayerObject [TransformObject]
Collider (Script)
Planetary_Jumper (Script)
PlanetObject [TransformPlanet]
Collider (Script)
Script with OnTriggerEnter
When they collide, You can set the PlanetaryJumper target property with the following script in [Script with OnTriggerEnter] :
function OnTriggerEnter (other : Collider) {
if (other.tag == "Player") {
motionCon = other.gameObject.GetComponent("Planetary_Jumper");
motionCon.targetPlanet = transform;
}
}
It is possible that i made some syntax errors since i come from C# background.
To be honest, I made a huge mistake in the hierarchy (after you told me to check the hierarchy) (Turns out it is placed in the deeper child object), and my script works fine afterall!, so your answer is legit, and this works too, I appreciate it!