- Home /
change variable in script attached to parent using triggers
I'm trying to make a simple sliding door prefab which opens when the player touches a trigger object, and closes when the player stops touching the trigger.
I have a scaled cube as the door mesh, and attached a script, "autoDoorScript" The collision object is a non-renderable sphere with a script "autoDoorSensorScript" attached. The mesh is the parent of the sphere, and both are then attached to a prefab, "autoDoorPrefab"
It works using a Static variable to control variables between scripts. When the player touched the collision trigger, it set canOpen = true; in the mesh object. However, because of the Static variable, all doors opened simultaneously.
What I'm trying to achieve is this: When the player steps within the sensor object, the "onTriggerEnter" of the "autoDoorSensorScript" is called. This needs to "canOpen = true;" in the "autoDoorScript" attached to the door mesh in the same prefab. I can't use a static variable, as don't want all other objects effected.
I've searched the forums etc pretty extensively, but the methods employed are beyond my current skills. Are there any simple solutions?
It seems it'd be a pretty regular requirement to do something like:
GetComponent(autoDoorScript).OpenAndClose();
If anyone wants to see my code, I'll post what I've got so far, but it's more a theoretical question. I know why I'm getting an error, just not how to fix it.
Thanks
my cube is literally just the cube primitive with some scaling to make it more door shaped. I needed a static initially to make the "canOpen" variable inside the "autoDoorScript" visible to the "onTriggerEnter" Event of the "autoDoorSensorScript"
That worked fine, like this:
function OnTriggerEnter(other:Collider)
{
if(other.gameObject.tag == "Player")
{
autoDoorScript.canOpen = true;
}
}
but then when I drag more than one door prefab into the scene, all of them open at the same time because the variable is static.
Thanks
Answer by DaveA · Aug 19, 2013 at 03:32 PM
It would help to see your cube, but why do you think you need a static? Just take static off that variable.
I'm also unsure what you mean by 'parent' Are you actually creating separate GameObjects and parenting them? Or do you mean, a component (script) attached to the door?
Because what I would do is: 1. Create door from cube by scaling. 2. Add sphere collider to same door object 3. Add your script to same door game object
Then make a prefab of that. Each door is self-controlled.
Ah I understand!
I'll give that a go but can forsee an issue as you can only have one collider on each object.
I'll need a sphere collider for the trigger and a box collider to prevent the player walking through whilst the door is opening.
How to get around that?
@Spider_newgent , converted two answers to comments, be $$anonymous$$dfull of where you respond. Do not write your response as an answer unless it is one. If it is not, you can add comments to your questions or to the answers that are given to follow up.
Understood.
Didn't see the button there. Thanks for pointing it out.
I'll take better care next time.
Just in case anyone else has the same/similar problem, it is possible to have more than one collider on an object..
Your answer
Follow this Question
Related Questions
Enemy trigger for player 1 Answer
Manually triggering a script from the editor (utility, macro etc.) 1 Answer
Door Opens When Not In Trigger And Sound Help 1 Answer
How would I go about removing a mesh renderer component on collision with a trigger? 1 Answer
Melee for 2.5D Platformer? (C#) 0 Answers