Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Merglasch · Jun 19, 2011 at 11:21 AM · collisiontriggerontriggerstay

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Merglasch · Jun 19, 2011 at 05:44 PM 0
Share

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...

avatar image Merglasch · Jun 19, 2011 at 06:08 PM 0
Share

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?

avatar image Merglasch · Jun 20, 2011 at 08:42 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Can someone clarifies OnTriggerEnter, OnTriggerExit and OnTriggerStay for me? (I have image included already) 0 Answers

OnTrigger event when colliders are already touching eachother 1 Answer

OnTriggerStay Weirdness 1 Answer

Detect when a trigger has entered a collider 1 Answer

OnTriggerStay still called after moving a parent gameobject 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges