- Home /
Activate script when player enters trigger zone.
Hello! I have the following script: private var Movements = new Array(); private var MovementIndex : int = 0; private var rewinding : boolean;
function Update ()
{
if(!rewinding)
{
Movements.Add(transform.position);
MovementIndex++;
}
if(MovementIndex > Movements.Count - 1)
{
MovementIndex = Movements.Count;
}
if(Input.GetKey(KeyCode.Tab))
{
rewinding = true;
Rewind();
}else
{
rewinding = false;
}
}
function Rewind ()
{
MovementIndex--;
transform.position = Movements[MovementIndex];
Movements.RemoveAt(MovementIndex);
}
I'm prety new to unity and im trying to figure out how to make this script attached to objects in the trigger zone only work when the player enters the trigger zone...any ideas? Thank you.
Answer by $$anonymous$$ · Mar 31, 2013 at 10:10 AM
do you mean something like this?
//attach this script to your trigger
var Player : GameObject;
var Script : theScript;
function OnTriggerEnter(other : Collider) //Check if something has entered the trigger ( and declares this object in "other" )
{
if(other.collider.tag == Player.tag) //Checks if the Player is inside the trigger
{
Script = GetComponent(theScript);
Script.enabled = true; //enables theScript.
}
}
You have to change "theScript" to the name of the script you want to enable. e. g. when you named your script "Toaster" it would be
var Scipt : Toaster;
Script = GetComponent(Toaster);
you can disable the script again if you copy-paste the script and change
function OnTriggerEnter(other : Collider)
to
function OnTriggerExit(other : Collider)
and
Script.enabled = true; //enables theScript.
to
Script.enabled =false; //this time it will disable the script
I hope i could help you :)
I'm using this script to fire up a random number generator (so when player enters a trigger zone, it will create a random number 0 - 17) but I keep getting:
"Assets/Scripts/Trigger.js(4,14): BCE0018: The name 'RandomNumberGen' does not denote a valid type ('not found'). Did you mean 'UnityEngine.RuntimeAnimatorController'?
Could this be because my Trigger script is JavaScript, and my RandomNumberGen script is C# ?
Answer by Frankank · Mar 31, 2013 at 09:29 PM
Or you can add a bool and say at the begining of the scripts:
if (example == true)
{
// your script
}
then you tag your collider and say OnTriggerEnter(Collider other) bla bla if (other.tag == namethething) example == true that's it.
P.S: this is in C#...
Your answer
Follow this Question
Related Questions
script countdown 2 Answers
activate script from trigger 1 Answer
How To Disable A Prefab? 2 Answers
Timer activates script 1 Answer
Enemy trigger for player 1 Answer