Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

how to handle many trigger colliders?

Discussion in 'Scripting' started by denali5x, Aug 24, 2017.

  1. denali5x

    denali5x

    Joined:
    May 13, 2017
    Posts:
    16
    so I am trying to develop a gearVR app, and and running into an issue.

    Basically, I have a long hall. In this hall are may triggers. Each trigger fades in/out description text.

    I can get 1 trigger to fade in/out its associated text. However, I cant get beyond that, because UnityEngine.Collider is already defined. You can see i tried to insert an if statement, but this causes nothing to happen. If i remove the if statement, then it works as intended.

    I guess the goal here is to try to figure out how to add a bunch of if statements, per collider. in the script.

    Can anyone help please.

    My script is... :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using DG.Tweening;
    5.  
    6. public class fadeText : MonoBehaviour {
    7.  
    8.  
    9.     void OnTriggerEnter (Collider col)
    10.     {
    11.         if (col.gameObject.name == "collider01") {
    12.             DOTween.PlayForward ("ID1");
    13.             Debug.Log ("entered");
    14.         }
    15.     }
    16.        
    17.  
    18.  
    19.  
    20.        void OnTriggerExit(Collider other)
    21.  
    22.         {
    23.            DOTween.PlayBackwards("ID1");  
    24.         }
    25.  
    26.  
    27.  
    28. }
    29.  
     
  2. Ingot

    Ingot

    Joined:
    Oct 8, 2011
    Posts:
    258
    I suppose that you have the fadeText script attached to your player controller.
    If you create a script that is designed to be attached to each trigger collider,
    each of them will be their own instance and you shouldn't have the same
    problem any longer. Have the script compare the col's tag or name to see
    if it is the player and then execute the tween action.

    If I am incorrect about this solution, I apologize.
     
    Kiwasi likes this.
  3. denali5x

    denali5x

    Joined:
    May 13, 2017
    Posts:
    16
    I have the Fade Text script on each collider. Do you suggest I place it on the player?

    I dont want to have to create (duplicate) the fade script per collider, seems overkill.

    I'll have about 20 colliders. Each collider has a small amount of text associated with it. When the player enter/exits the collider, the text appears/disappears.

    I guess i need a way to name each collider and detect if the player is in it or not, and execute the associated fade.
     
  4. Ingot

    Ingot

    Joined:
    Oct 8, 2011
    Posts:
    258
    I think you have it set up correctly, but you need a way to distinguish the text associated with each collider.
    I suggest exposing the text or string variable associated with the displayed text in the inspector and typing
    it in for each collider. I am sure there are other ways, but that is how I would handle it. Something like this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using DG.Tweening;
    5.  
    6. public class fadeText : MonoBehaviour {
    7.  
    8. [SerializeField]
    9. string displayTextForThisCollider;
    10.  
    11.     void OnTriggerEnter (Collider col)
    12.     {
    13.         if (col.gameObject.name == "Player") {
    14.             DOTween.PlayForward (displayTextForThisCollider);
    15.             Debug.Log ("entered");
    16.         }
    17.     }
    18.      
    19.        void OnTriggerExit(Collider other)
    20.         {
    21.            DOTween.PlayBackwards(displayTextForThisCollider);
    22.         }
    23. }
     
    Kiwasi likes this.
  5. denali5x

    denali5x

    Joined:
    May 13, 2017
    Posts:
    16
    Well, I'm unsure exactly what that does, however, I appreciate the response. I used the above and the result I was seeking did not function. I've done a bit of research on [SerializeField] to try to educate myself on how and why you chose to use it here...and it took me down a rat-hole that seemed to stray in an completely different direction with private class, etc.

    Thanks.
     
  6. Ingot

    Ingot

    Joined:
    Oct 8, 2011
    Posts:
    258
    [SerializeField] is an attribute and it allows you to expose the variable in the editor / inspector. Then you will be
    able to type the text messages for each collider by clicking on them and looking at the fadeText script in the
    inspector. By assigning the text for each collider to a variable, you can edit the text for each without having
    to open the script in monodevelop, and you can pass the text as a parameter to DoTween. It is just a more
    sensible approach to coding that little task.
     
  7. denali5x

    denali5x

    Joined:
    May 13, 2017
    Posts:
    16
    Ohhhh!...I get it now. Ya I think this is exactly what I needed. Ive just tested it on 2 elements and seems to be working as intended so far.

    Thanks.
     
  8. Ingot

    Ingot

    Joined:
    Oct 8, 2011
    Posts:
    258
    Great!
     
  9. denali5x

    denali5x

    Joined:
    May 13, 2017
    Posts:
    16
    OK, how would I pull this off in 3D?

    Ive created this so far, I thought it would work, but nogo

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.EventSystems;
    5. using DG.Tweening;
    6.  
    7. public class fadeText : MonoBehaviour{
    8.  
    9.     [SerializeField]
    10.     string displayTextForThisCollider;
    11.  
    12.     void OnPointerEnter(PointerEventData data)
    13.     {
    14.         DOTween.PlayForward (displayTextForThisCollider);
    15.         Debug.Log("entered");
    16.     }
    17.  
    18.     public void OnPointerExit(PointerEventData eventData)
    19.     {
    20.         DOTween.PlayBackwards(displayTextForThisCollider);
    21.         Debug.Log("exit");
    22.     }
    23. }
    24.  
    any idea?

    Thanks
     
  10. Ingot

    Ingot

    Joined:
    Oct 8, 2011
    Posts:
    258
    Can you elaborate on the scenario? Where is the pointer when you want to track it for pointer enter?
    Is the mouse pointer free and moving with the mouse controller or is it locked to the center of the screen?
     
  11. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,214
    You need to add the interfaces to the declaration if you want to be able to use the pointer functions:

    Code (csharp):
    1. public class fadeText : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {
     
  12. denali5x

    denali5x

    Joined:
    May 13, 2017
    Posts:
    16
    the scenario is basic. think of a web page. I have 10 buttons going down the left side of the app. The app is a standalone, 1280px by 800px. All 2D. The buttons are the unity built in ones. I Though I could just port the above script over to 2D, but it eludes me.

    The idea is when you mouseover the button, to the right, a canvas fades in, displays a few sentences, and onMouseOut, the canvas fades out. I need to do this for 10 buttons. Kinda like a tooltip, but with much more text. No functionality in this canvas, just a description

    Is this a totally different approach?