- Home /
Multiple colliders (triggers) in same script?
Hi all, I am working on a project that involves the character experiencing events when entering colliderboxes. The colliderboxes are triggers that send an int over UDP protocol.
My objective is to have 5 colliderboxes that each sends an int if the character enters the colliders.
I am, however, not very successful, and I need immediate assistance, as I am not as good in programming as the majority in the forums. This is my script:
using UnityEngine; using System.Collections; using System; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; using System.Collections.Generic;
public class NickThesisTriggerManager : MonoBehaviour { private static NickThesisTriggerManager instance;
public static int SendValue1 = 20; public static int SendValue2 = 30;
// Use this for initialization
void Start () {
instance = this;
init ();
}
// Triggers are placed on the collider boxes
void OnTriggerEnter(Collider other){
if (GameObject.Find("Trigger1") != true)
{
return;
}
else
{
Debug.Log("PLAYER HAS ENTERED TRIGGER 1");
SendUDPMessage(SendValue1);
}
if (GameObject.Find("Trigger2") != true)
{
return;
}
else
{
Debug.Log("PLAYER HAS ENTERED TRIGGER 2");
SendUDPMessage(SendValue2);
}
}
Oh boy.....is this a school project? When the collider enters all you're doing is calling gameobject.find, which returns a gameobject not a Boolean. So it will always not equal true and send the message. You need to get the gameobject info out if the collider called other and detect which object it is
Answer by koray1396 · Apr 01, 2014 at 05:12 PM
I think you want something like this;
void OnTriggerEnter(Collider other){
if(other.transform.name == "Trigger1"){
Debug.Log("PLAYER HAS ENTERED TRIGGER 1");
SendUDPMessage(SendValue1);
} else if(other.transform.name == "Trigger2"){
Debug.Log("PLAYER HAS ENTERED TRIGGER 2");
SendUDPMessage(SendValue2);
}
}
Your answer
Follow this Question
Related Questions
Terrain, Player falls through.... but only some parts. 0 Answers
collision detection not working? 1 Answer
gameobject destroyed on collision with character 3 Answers
Simple AI and Animation 2 Answers