- Home /
enable script on trigger
hey, I want to figure out how I can enable the player's shooting script when he walks into a trigger. here is the shooting script that I want to be enabled:
var bullet : Rigidbody; //the bullet we are shooting must have a rigidbody var Speed = 32000; //the speed the bullet is shot at
private var shooting = false; //this is only used if rapid fire is set to true //RateOfFire private var Counter = Time.deltaTime; var RateOfFire = 0.5;
function FixedUpdate (){
if(Input.GetButtonDown("Fire1")){
shooting=true;
}
if(Input.GetButtonUp("Fire1")){
shooting=false;
}
if(shooting==true){
Counter += Time.deltaTime;
if(RateOfFire < Counter){
var shotRapid =Instantiate(bullet, transform.position, Quaternion.identity);
shotRapid.GetComponent.<Rigidbody>().AddForce(transform.forward * Speed);
Counter=0;
}
}
}
***this is not my script and i have no knowledge in coding. sorry lol.
Well assu$$anonymous$$g you have a trigger collider set up for such purposes, you could just do this...
public $$anonymous$$onoBehaviour BehaviourToEnable;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
BehaviourToEnable.enabled = true;
}
}
or a more elegant, and reusable solution.
public UnityEngine.Events.UnityEvent onTriggered;
public string TagToTrigger;
void OnTriggerEnter(Collider other)
{
if (other.tag == TagToTrigger)
{
onTriggered.Invoke();
}
}
Simply setup the script.enabled function in the inspector event manager and it will work as well.
Answer by Ratchet_Ritz · Mar 24, 2018 at 04:04 AM
@RobAnthem sorry, I get a compiler error for the first script:
Unexpected symbol 'MonoBehaviour', expecting 'class', 'delegate', 'enum', 'interface', 'partial', or 'struct'
Well as you said, you don't know how to code, but this isn't really the place for people to write code for you. However, I'll forgive that you don't know the structure of a class file. So a straight copy and paste solution...
using UnityEngine;
public class TriggerEnabler : $$anonymous$$onoBehaviour
{
public $$anonymous$$onoBehaviour BehaviourToEnable;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
BehaviourToEnable.enabled = true;
}
}
}
thank you my man... I changed BehaviourToEnable to my shooting script and it worked.
Your answer

Follow this Question
Related Questions
Activate camera script with a trigger 1 Answer
Collison detection not working? 1 Answer
Play animation of FPS player after hitting trigger 2 Answers
Want a script that can instantiate an object when MOVING inside the trigger 1 Answer
Problem with spawning with companion AI,Script not working properly when GameObject reactivated? 0 Answers