- Home /
OnTriggerEnter Script only working on duplicated object
I have one script that makes an object move from one target, then back to the first target. This script also has a bool that the second script accesses. The second script is the trigger, so when the player enters the trigger, the first scripts bool is true, and the object moves to point a, when the player leaves the trigger, the object moves back to point b.
public class Platform_Up : MonoBehaviour { public bool triggered; public Transform target_a; public Transform target_b; public float platform_speed;
void Start()
{
//triggered = false;
}
void Update()
{
if(triggered)
{
float step = platform_speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target_a.position, step);
}
if(!triggered)
{
float step = platform_speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target_b.position, step);
}
}
}
The problem is when i duplicate the object, theres 2 triggers. No matter which objects trigger i enter, the duplicated object only moves. The same happens if i duplicate again, the third one only moves.
public class Platfor_Up_Trigger : MonoBehaviour {
void OnTriggerStay2D(Collider2D other)
{
if(other.tag == "Player" || other.gameObject.tag == "Spawned Square")
{
GameObject platform = GameObject.Find("Platform_Moving_Up");
Platform_Up platform_script = platform.GetComponent<Platform_Up>();
platform_script.triggered = true;
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Player" || other.gameObject.tag == "Spawned Square")
{
GameObject platform = GameObject.Find("Platform_Moving_Up");
Platform_Up platform_script = platform.GetComponent<Platform_Up>();
platform_script.triggered = false;
}
}
}
I was wondering if there was a way to only have the object im triggering move, and not the rest of the duplicated objects
Answer by Dragate · Oct 04, 2017 at 01:03 PM
If I understood correctly, these two scripts are on the same gameobject. It seems to me that GameObject.Find("Platform_Moving_Up") from all duplicated objects get reference to the same platform, which is the only one working.
In Platfor_Up_Trigger class (both in triggerStay and triggerExit), could you delete
GameObject platform = GameObject.Find("Platform_Moving_Up");
and change
Platform_Up platform_script = platform.GetComponent<Platform_Up>();
to
Platform_Up platform_script = gameObject.GetComponent<Platform_Up>();
? Let's see if that works out for you.
Hi sorry for the late reply and thanks for the help. I tried that but now its object not set to an instance of an object when the trigger is entered and exited.
If it can't find the "Platform_Up" script, then the 2 scripts(Platfor_Up_Trigger & Platform_Up) are not on the same gameObject? Could you clarify where each script is attached to exactly?
A guaranteed solution is to make platform_script a public variable in the Platfor_Up_Trigger class and assign manually (via Inspector) the platform you want to be referenced to each time you create a new platform.
Your answer
Follow this Question
Related Questions
How to make audioclip reference to another script 0 Answers
How to assign one script on different gameobject of 1 prefab and make them run one by one 0 Answers
Point Counter Works Only Once! 1 Answer
I have overrided a built-in script accidentally and not sure how to fix 2 Answers
The spaceship acceleration script is good ? And how to use it with engine ? 0 Answers