- Home /
Box collider with strange behaviour
I have 5 objects in my scene. Each of them have a box collider
(`IsTrigger` selected) in front of them. I also have a menu in a GUITexture
. I want to show the menu if the box in front of the object is triggered and be able to rotate the object (that has the box just triggered). My problem is that if i assign the same GUITexture
to my objects, all of them are rotating if one is triggered.
If i print myGameObject.name
in the Start()
all 5 objects are printed at once.
Does anyone have an idea on how to fix this ?
The class that implements my idea is :
public class Trigger : MonoBehaviour {
public GameObject myGameObject ;
public GUITexture myGuiTexture;
private Vector3 rotation;
private Quaternion initialRotation;
private GameObject go;
void Start(){
myGuiTexture.enabled = false;
initialRotation = myGameObject.transform.rotation;
go = new GameObject("Details guiText");
go.AddComponent(typeof(GUIText));;
go.guiText.enabled = false;
}
void Update () {
if (myGuiTexture.enabled == true){
if (Input.GetKeyUp(KeyCode.Alpha1) || Input.GetKeyUp(KeyCode.Keypad1))
myGameObject.transform.rotation = initialRotation;
if (Input.GetKey(UnityEngine.KeyCode.Keypad1) || Input.GetKey(UnityEngine.KeyCode.Alpha1)){
rotation = new Vector3(0, Input.GetAxis("Mouse X"), 0);
myGameObject.transform.Rotate(rotation);
}
}
}
void OnTriggerEnter(){
myGuiTexture.enabled = true;
}
void OnTriggerExit(){
myGuiTexture.enabled = false;
}
}
yes. but the problem is not with the object (i think) because if i use different guitextures
it works fine.
Answer by rhose · Jun 12, 2013 at 06:39 PM
I found the solution in the mean time. The problem was in the update
function. The solution is presented below :
public class Trigger : MonoBehaviour {
public GameObject myGameObject ;
public GUITexture myGuiTexture;
private Vector3 rotation;
private Quaternion initialRotation;
private GameObject go;
private bool sw; //a switch variable to know if the box collider was triggered
void Start(){
myGuiTexture.enabled = false;
sw = false; //the box was not triggered
initialRotation = myGameObject.transform.rotation;
go = new GameObject("Details guiText");
go.AddComponent(typeof(GUIText));
go.guiText.enabled = false;
}
void Update () {
if (myGuiTexture.enabled == true && sw){ //if sw==true
if (Input.GetKeyUp(KeyCode.Alpha1) || Input.GetKeyUp(KeyCode.Keypad1))
myGameObject.transform.rotation = initialRotation;
if (Input.GetKey(UnityEngine.KeyCode.Keypad1) || Input.GetKey(UnityEngine.KeyCode.Alpha1)){
rotation = new Vector3(0, Input.GetAxis("Mouse X"), 0);
myGameObject.transform.Rotate(rotation);
}
}
}
void OnTriggerEnter(){
myGuiTexture.enabled = true;
sw = true; //now the player collides
}
void OnTriggerExit(){
myGuiTexture.enabled = false;
sw = false; //the player is not still colliding
}
}
If i use the same GUITexture
on all my objects i don't know which gameobject
has enabled the texture. That's why all my objects were reacting to the rotation.