enabling a script by entering a trigger
Sorry in advance ,I looked all day in Unity Answers and on the forums but could not find the answer I'm looking for.
All I want to do is activate a script that is attached to an object when the player enters the trigger, and disable it when the player leaves the trigger.
I've tried various scripts that seemed close to what I needed to do ,but got compiler errors when I tried them.
Can anyone help me please?
line 12 is this: ConstantRotate.enabled = true;
But after closing unity nd reopening I'm no longer getting the error message ,but nothing happens either.
I don't have constant rotate attached to the player because the object is supposed to rotate when the player enters the trigger.
Answer by dCalle · Jan 29, 2017 at 10:53 PM
i think that would be something like:
void OnTriggerEnter(Collision other)
{
if (other.gameObject.tag == "Player")
{
MyScript myScript = other.gameObject.GetComponent<MyScript>();
//You could check if you really attached MyScript here, but I think we skip that. ;-)
myScript.enabled = true;
}
}
void OnTriggerExit(Collision other)
{
if (other.gameObject.tag == "Player")
{
MyScript myScript = other.gameObject.GetComponent<MyScript>();
myScript.enabled = false;
}
}
Thanks for the response, I appreciate it. This is javascript right?
I get the following error:
Assets/scripttriggertest.js(1,41): BCE0043: Unexpected token: ).
ok so it's C# ,I have it set up like this:
using UnityEngine; using System.Collections;
public class scripttrigger : $$anonymous$$onoBehaviour {
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
ConstantRotate ConstantRotate = other.gameObject.GetComponent<ConstantRotate>();
//You could check if you really attached $$anonymous$$yScript here, but I think we skip that. ;-)
ConstantRotate.enabled = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
ConstantRotate ConstantRotate = other.gameObject.GetComponent<ConstantRotate>();
ConstantRotate.enabled = false;
}
}
}
I get no errors at all but nothing is happening as of yet, when I ebter the trigger. Any ideas what I might be doing wrong?
is it 2D or 3D?
do you have your (not the player's) collider set as trigger?
do you have a collider on both your player and where you want your trigger?
actually I am getting one error when entering the trigger:
NullReferenceException: Object reference not set to an instance of an object scripttrigger.OnTriggerEnter (UnityEngine.Collider other) (at Assets/scripttrigger.cs:12)
This is for 3d.
Yes I have a cube with a box collider attached to the object set to IS trigger. The 3rd person controller has a capsule collider and is tagged as player.
Very sorry ,I didn't mean to post as an answer, I meant to post as a comment.
Where is line 12 in the script?
Did you froget to attach constant rotate to your player?
line 12 is this: ConstantRotate.enabled = true;
But after closing unity nd reopening I'm no longer getting the error message ,but nothing happens either.
I don't have constant rotate attached to the player because the object is supposed to rotate when the player enters the trigger.
Answer by brandon333 · Apr 18, 2020 at 06:49 AM
It only Works if the script you're trying to manipulate is attached to the player.
Your answer
Follow this Question
Related Questions
Camera being affected by sphere collider triggers 0 Answers
Trigger a box collider based on force of an object 1 Answer
Falling Through Floor after a certain number of Triggers... 0 Answers
How do I make trigger destroy an object,with the collision from the player 1 Answer
How Do I increase speed based off of a trigger collider? 0 Answers