- Home /
OnTrigger problems with a sphere enabling/disabling targets on collision
Hi all, I'm quite new to c# and unity so please excuse my maybe noobish question^^
Assumed Problem: OnTriggerExit doesn't get called, although sphere is moved away.
Details:
I'm creating kind of a strategy map, where multiple players(2 at the moment) can move to defined places(towns) in a turn-based manner.
I'm using a sphere trigger with attached rigidbody and a plane for each town(also triggers) to see which of the towns are close enough to move to. The sphere is set to the position of the player, who is changed when he reaches his target. The towns in reach are lit up for the player to see where he can move to.I have 2 scripts for this. One is Bodenhaftung.cs (not attached I think) and the other one is at the moment still called newBehaviourScript (attached to every plane/town in the scene).
Most part of the script works well, but the one town the other player last arrived at doesn't get disabled after the player switch. I checked and saw that OnTriggerExit doesn't get called even though the sphere doesn't touch the plane anymore. What's even better: OnTriggerStay still seems to get called... I tried the whole thing with OnTriggerEnter but then I can't seem to move to places where the other player could also move to...
I'm very confused here so I'd be glad to get some help^^ If you need any further info, just tell me^^
Also I'd like to know why this isn't working because I'd like to fix things myself and learn something from this mistake not let my work done by others^^ thank you in advance
Code:
public class Bodenhaftung : MonoBehaviour {
public static Component Grund;
public static GameObject Spieler;
public static GameObject Kugel;
public static GameObject Ziel;
public static int Zielpunkt;
public static float distanz;
// Use this for initialization
public void Start () {
Spieler = GameObject.Find("Constructor");
Kugel = GameObject.Find("Sphere1");
distanz = 0;
}
// Update is called once per frame
void Update () {
}
}
public class NewBehaviourScript : Bodenhaftung {
public Color colortrans=Color.black;
IEnumerator distrechner()
{
while (Zielpunkt==1)
{
float translation = Time.deltaTime * 10;
Spieler.transform.Translate(0, 0, translation);
distanz = Vector3.Distance(Ziel.transform.position,Spieler.transform.position);
if (distanz >= -1 && distanz <=1) {
Zielpunkt = 0;
if (Spieler == GameObject.Find("Constructor"))
{
Spieler = GameObject.Find("Constructor2");
}
else
{
Spieler = GameObject.Find("Constructor");
}
yield return Zielpunkt;
break;
}
yield return 0;
}
}
void OnMouseDown(){
if (distanz >= -1 && distanz <=1 && renderer.enabled==true)
{
Ziel = gameObject;
Spieler.transform.LookAt(Ziel.transform, Vector3.up);
Zielpunkt = 1;
StartCoroutine(distrechner());
}
}
void OnTriggerStay(Collider Kugel)
{
if (Zielpunkt == 0)
{
if (Ziel == gameObject) renderer.material.SetColor("_Color", Color.red);
else renderer.material.SetColor("_Color", Color.green);
renderer.enabled = true;
}
else
{
renderer.enabled = false;
}
}
void OnTriggerExit(Collider Kugel)
{
renderer.enabled = false;
}
void Update(){
Kugel.transform.position = Spieler.transform.position;
}
}
EDIT: Added a picture so it's a little bit easier to see
http://imageshack.us/photo/my-images/841/mapdq.jpg/
As you can see the guy in the left sees his possible targets but also the last position his counterpart went to (up right).
Uploading a picture seems to be tricky...any hints?
Answer by realm_1 · Jun 19, 2011 at 03:24 PM
I think you are triing to access to a variable from the Bodenhaftung to the NewBehaviourScript script I don't code with c# but if you want to do something like this you have to write for example:
Bodenhaftung.Ziel = gameObject;
well I thought I reached that goal with making the other variables static since they get changed (Players switch, targeting works etc.) So it's unlikely he can't access these variables, isn't it? But I'll try this approach anyway. Let's see if it works out...
Doesn't seem to change anything. I guess since newBehaviourScript inherits from Bodenhaftung and these variables are static they can get accessed somehow already. Noticed one thing though. At the OnTrigger functions I can't replace OnTriggerStay(Collider $$anonymous$$ugel) with OnTriggerStay(Collider Bodenhaftung.$$anonymous$$ugel). It gives me 29 different errors^^. Also I can't remove the static prefix because it gives me some "object reference required" error. So maybe another approach?
Could the object reference error be solved if I assign bodenhaftung.cs to some empty gameobject in the world editor? Also I guess my problem is that my parameter $$anonymous$$ugel in OnTrigger isn't my gameObject $$anonymous$$ugel. Will implement a check for name which should hopefully fix this. Will try this as soon as I get home
Your answer
