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… :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class fadeText : MonoBehaviour {
void OnTriggerEnter (Collider col)
{
if (col.gameObject.name == "collider01") {
DOTween.PlayForward ("ID1");
Debug.Log ("entered");
}
}
void OnTriggerExit(Collider other)
{
DOTween.PlayBackwards("ID1");
}
}
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.
1 Like
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.
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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class fadeText : MonoBehaviour {
[SerializeField]
string displayTextForThisCollider;
void OnTriggerEnter (Collider col)
{
if (col.gameObject.name == "Player") {
DOTween.PlayForward (displayTextForThisCollider);
Debug.Log ("entered");
}
}
void OnTriggerExit(Collider other)
{
DOTween.PlayBackwards(displayTextForThisCollider);
}
}
1 Like
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.
[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.
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.
OK, how would I pull this off in 3D?
Ive created this so far, I thought it would work, but nogo
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using DG.Tweening;
public class fadeText : MonoBehaviour{
[SerializeField]
string displayTextForThisCollider;
void OnPointerEnter(PointerEventData data)
{
DOTween.PlayForward (displayTextForThisCollider);
Debug.Log("entered");
}
public void OnPointerExit(PointerEventData eventData)
{
DOTween.PlayBackwards(displayTextForThisCollider);
Debug.Log("exit");
}
}
any idea?
Thanks
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?
You need to add the interfaces to the declaration if you want to be able to use the pointer functions:
public class fadeText : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {
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?