I am working on a little project, and i have a door script attached to my player which look like this:
var closeTime = 8;
var openTime = 3;
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "door")
{
yield WaitForSeconds(openTime);
hit.gameObject.animation.Play("door_open");
yield WaitForSeconds(closeTime);
hit.gameObject.animation.Play("door_close");
}
}
Which works fine, but what i miss is some kind of effects (particles) to appear when the door opens. So my question is how is the best way to do that? Is it even possible to do in this script, since it is attached to the player, and the particles is suppose to emit at the door? At the moment I have started a code which is attached to the door, it contains:
public var doorParticles : ParticleEmitter;
doorParticles.particleEmitter.emit = false;
(The door also have a particle attached to it, and the code above prevents it from emit as the game starts)
You can use GetComponent to retrieve the script component of the door, then you can access doorParticles. Or use a method like “OnOpen” on your door script that hides the details of what the door does when it opens. But it might make more sense to make the door detect the hit instead of the player.
Like Jasper said, you can use GetComponent to access the public Open field.
// To access public variables and functions
// in another script attached to the same game object.
var script : ScriptName;
script = GetComponent("ScriptName");
script.DoSomething ();
or
var script : PlayerScript;
script = GetComponent("Player1");
if (script.Open == false)
{
}
I’m not sure if that’s syntactically correct, as I write my stuff in C#, but it’s close enough to give you the general idea
Just because you declared your Open field to be public in the player script, does not mean that it is globally public and can be accessed by other scripts.
Your door will need to get a reference to the player script, thus the reason you need to use GetComponent, as it will provide the reference for you.
Once you have a reference to your door, you can access the Open field from your player script via the aforementioned example.