- Home /
Title letters appearing one after another
I want to set some objects inactive when my scene starts and I want to enable them consequently after some time (basically it's a title and I want each letter to appear a few milliseconds after the previous one).
I've made each letter a separate GameObject, since I want to rotate them too. I suppose there is the "wrong" way of attaching a single script to each letter individually and setting a timer to each script.
I thought of doing it more "correctly", by adding BoxCollider2D components to each letter and having an empty GameObject with a BoxCollider2D moving through my screen, enabling each letter one at a time.
My Start method is
void Start () {
GameObject[] titleElements = GameObject.FindGameObjectsWithTag ("Title");
foreach (GameObject titleElement in titleElements)
titleElement.SetActive(false);
}
and it works fine, since I've tagged each of my letters as "Title", when I start my scene all these objects get deactivated.
In the Update method I move my empty "trigger" object horizontally in my scene, all of my objects have collider2D components with "isTrigger" enabled and my trigger object also has a RigidBody2D component. I had followed some tutorial on how to make a 2D game (clonybird if anyone is familiar) and it turns that I have to set my RigidBody2D Gravity to 0 and disable isKinematic or else my trigger object won't collide with the other colliders.
But my letters don't seem to collide with my trigger. Maybe this has to do with the fact that they are children to a camera which is a part of an NGUI-based GUI and I'm not 100% familiar with the way this works yet, I use it to have a resolution-independent UI.
I created an empty Game object with a BoxCollider2D object to test my logic and now my trigger triggers both this empty GameObject and a single Gameobject of my title, which is not the first it collides to. I really cannot understand what is happening.
I know it's a very long post, but if anyone makes anything out of it I would be grateful..
I'd argue attaching a script to each letter is a perfectly fine approach.
If I were to approach this problem I would write a single $$anonymous$$onoBehaviour that has reference to all of the letters via an array field.
This $$anonymous$$onoBehaviour would then randomly begin the animation of each letter over a fixed period of time.
I think I did some of that in my start method
GameObject[] titleElements = GameObject.FindGameObjectsWithTag ("Title");
The problem is that they are in random order inside the array.
I temporarily solved my problem by adding separate empty GameObjects with colliders and manually activating each object (in the right order) when my trigger object collides with these.
I know this is not the right way to code, but at least it works.
I still can't understand why it doesn't collide with the colliders of the letter objects within NGUI ui..
Answer by fafase · Apr 03, 2014 at 10:50 AM
Why wouldn't you use an easy GUI.Box?
string title = "My Game";
string str = "";
public float freq = 1.0f;
void Start(){
StartCoroutine(AddLetter());
}
void OnGUI(){
GUI.Box(rect, str);
}
IEnumerator AddLetter()
{
float timer = 0f;
int index = 0;
while(index < title.Length){
while(timer < freq){ // freq defines how fast letters show up
timer += Time.deltaTime;
yield return null;
}
str += title[index];
index++;
timer = 0;
}
}
I've read a few comments advising to stay away from OnGUI, so I thought of using NGUI, to ensure easiest resolution-independent UI.
I will look through your code though to check the logic, thanks...
Yes, you should avoid GUI mostly for in game GUI. But if you have a situation with little happening like a front page menu, GUI is fine to my taste. And GUI will slow down if you abuse it on a slow computer or if you have tens of indicator on a mobile game.
I have yet to experience any problem using GUI.
Your answer
Follow this Question
Related Questions
Why is this only working for the second object that collides? 1 Answer
Can't click gameobject when over another trigger? 1 Answer
Box Collider 2D "Is Trigger" Help! 1 Answer
How to get box collider detection by Ray cast only from the outside of the box collider 1 Answer
BoxCollider2D, Trigger not working 1 Answer