Super good tutorial. I've been struggling with applying text effect in TMP, and this tutorial came in clutch! It's extremely quick, easy to understand, and straight to the point! Thank man! You really helped me and others out!
Oh friend, I'm no Brackeys that's for sure. We cannot replace the legends that came before us but we do our best to live up to them. But hey, if there's anything you'd like me to cover in a tutorial, give me a shout!
@@Madalaski true true! None cud replace em but teaching others the stuff u know is just so humane and humble of y'all imo! Ps idk what certain topic to point out in Unity but a series on the current ways of networking in it wud be immensely helpful (if it's within the scope of your interests of course)! Anyway keep the Shader tuts flowing in and with it, anything Unity would be welcomed by the majority I guess XD! Great job senpai!
Thank you so much for this tutorial! *But I would highly recommend skipping spaces:* for (int i = 0; i < charCount; i++) { TMP_CharacterInfo c = textMesh.textInfo.characterInfo[i]; // Skip spaces as they don't have any vertices. // // They also have a vertexIndex of zero which causes confusing // issues with modifications to the first letter. if (c.character == ' ') { continue; } ... }
@@Madalaski I'm trying to do an effect where I display a score counter, and each time a number of the score changes, I play a little animation for that character. The characters that stay the same don't animate.
There is very little documentation about this, and it is very well explained in your video! It has helped me a lot, however it is still difficult for me to implement it by characters separately... You could upload more tutorials and different types of animations when typing texts, it would be very useful!
This tutorial was incredible thank you! I took the liberty of modifying the code a bit to add the ability to apply this effect to only one part of your whole text! (needed it for my game) and added some quality of life bools :) (again, I modified Madalaski code so props to him!) Here is the complete code for anyone that wants it: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; public class RainbowText : MonoBehaviour { //Variables for Word Change [SerializeField] bool applyOnlyToSelection; [TextArea(5,5)] //make this area bigger if you need to add more lines [SerializeField] string textToChangeColor; int specificWordIndex; //Variables for Text Change TMP_Text textMesh; Mesh mesh; Vector3[] vertices; List wordIndexes; List wordLengths; //Wobbling Variables Vector3 offset; [SerializeField] bool wobbleText = false; [SerializeField] float wobblingSpeed = 1f; //global Variables TMP_Text tempTextMesh; string tempString; //Color Variables [SerializeField] float colorSpeed = 2f; public Gradient rainbow; // Start is called before the first frame update void Start() { textMesh = GetComponent(); wordIndexes = new List(); wordLengths = new List(); if (applyOnlyToSelection) { char[] tempChars = textToChangeColor.ToLower().ToCharArray(); tempString = textMesh.text.ToLower(); for (int i = 0; i < tempString.Length; i++) { if(tempString[i].CompareTo(tempChars[0]) == 0) //check if the first letter of the Selection is found { for (int j = 1; j < tempChars.Length; j++) //Go over every character in our selection { if(tempString[i + j].CompareTo(tempChars[j]) != 0) //if next character is not the character in our selection, go back to the 1st For Loop { break; }
if(j == tempChars.Length - 1) //if every character was correct, We found our selection!! { print("Found the word!"); wordIndexes.Add(i); wordLengths.Add(tempChars.Length); tempString = textToChangeColor; return; } } } //Our selection was not found, Script will deactivate itself because it wont do anything anyways if (i == tempString.Length - 1) { print("Word not found!"); this.enabled = false; } } } else { wordIndexes.Add(0); //wordIndexes needs to start with an index of 0 tempString = textMesh.text; for (int index = tempString.IndexOf(' '); index > -1; index = tempString.IndexOf(' ', index + 1)) { wordLengths.Add(index - wordIndexes[wordIndexes.Count - 1]); wordIndexes.Add(index + 1); } wordLengths.Add(tempString.Length - wordIndexes[wordIndexes.Count - 1]); } } // Update is called once per frame void Update() { textMesh.ForceMeshUpdate(); mesh = textMesh.mesh; vertices = mesh.vertices; Color[] colors = mesh.colors; for (int w = 0; w < wordIndexes.Count; w++) { int wordIndex = wordIndexes[w]; if(wobbleText) offset = Wobble(Time.time * wobblingSpeed + w); for (int i = 0; i < wordLengths[w]; i++) { TMP_CharacterInfo c = textMesh.textInfo.characterInfo[wordIndex + i]; int index = c.vertexIndex; if (applyOnlyToSelection) { if (index == 0) continue; } //if there is a blank character, go to the next character instead colors[index] = rainbow.Evaluate(Mathf.Repeat(Time.time * colorSpeed - vertices[index].x * 0.001f, 1f)); colors[index + 1] = rainbow.Evaluate(Mathf.Repeat(Time.time * colorSpeed - vertices[index + 1].x * 0.001f, 1f)); colors[index + 2] = rainbow.Evaluate(Mathf.Repeat(Time.time * colorSpeed - vertices[index + 2].x * 0.001f, 1f)); colors[index + 3] = rainbow.Evaluate(Mathf.Repeat(Time.time * colorSpeed - vertices[index + 3].x * 0.001f, 1f)); if (wobbleText) { vertices[index] += offset; vertices[index + 1] += offset; vertices[index + 2] += offset; vertices[index + 3] += offset; } } } mesh.vertices = vertices; mesh.colors = colors; textMesh.canvasRenderer.SetMesh(mesh); } Vector2 Wobble(float time) { return new Vector2(Mathf.Sin(time * 3.3f), Mathf.Cos(time * 2.5f)); } }
This is a really cool solution! Mad props! I imagine an easier method would be to iterate through the WordInfo information in TextInfo property. Then you could just compare each word in there to the word you're trying to animate rather than having to go character by character. Or if you set the index of the word you want to animate you could go straight there with TextInfo.WordInfo[w].GetWord() but I don't know if that's better for your purposes or not.
@@Madalaski Thank you! That is actually the first route I took (WordInfo[w].GetWord()) but was having trouble getting it to work properly, I definitely think it could be improved with that, I just took this route Im more familiar with, still fairly new to all the possibilities with TMPro :)
This is really cool! Thank you! I did notice a bug, though: If the first letter in the text is part of the selection it is skipped. Still, this is really nice. I think I'll modify it a bit by moving it inside a Coroutine and use it in my current project.
Great tutorial, looks great :) Only weird thing is that for some reason the first letter of a paragraph moves WAY more than all of the other ones, only sometimes though...
Nice video, thanks! I was having a trouble making an effect for character because for some reason the first character always seems to take double the effect. If anyone having the same issue the fix is easy: instead of making a looping through the characters of the sentence, I just did a loop through all vertex: for (int i = 1; i < text.mesh.vertexCount; i++) { vertices[i] += Wobble(); } The rest is the same and it works like a charm. Also, I did a fast test and it seems the loop is always the same size, doesn't matter how long or short your text is.
do you know a way that I can call these effects using tags in textmeshpro... would be cool to have specific words wobble instead of the whole objects string...
This is a very good straight to the point tutorial! However I spotted a weird artifact appearing in the very center of the TMP asset (at the position of the TMP [T] gizmo). You can sort of see it at 1:41 at the top left corner of the 'm' in "some". When inspected closely it's a square mesh wobbling with the text, it doesn't matter if there's any text near the area, just appears at the very center. Sometimes this artifact disappears when I add a few more characters and sometimes it itself shows as a blob of characters... Any ideas what might cause this? Thanks!
Hmmm, if I think I noticed this problem a couple months later but I can't remember the exact suction. I believe it has something to do with a character being added to overall text but the new character didn't have it's new vertices set, creating an artifact. Might be worth to create a buffer zone of some kind. One of my later videos has code for my Code Animator software in the description. I believe that uses a better algorithm if you want to check that out.
If you're still encountering this issue, I've just found a solution, so even if you've fixed it on your own I'll leave it here for people who come after ^u^ It looks like this artifact is where "extra" characters are stored. Instead of creating and destroying a bunch of meshes, the engine just creates a mesh when needed and stores it very very small when it's not being used. However when messing with the vertices, you manually make these "extra" stored meshes visible by giving them scale. Accessing the vertices on a character-by-character basis instead of vertex-by-vertex basis should fix the issue, since you aren't accessing the extra stored meshes this way.
Great video, straight to the point! But I didn't get the part at 1:38... I am using a non-UI, world space TextMeshPro component and the mesh gets all deformed. Obviously there is no canvas renderer for that, but there's also no "SetMesh" method. What am I supposed to assign in order for the modified mesh to work with world space text?
ok so these text effects are AMAZING! there is one niche problem that i am having though, when the text is scrolling through with the typewriter effect, the wobble and colour effects seem to jitter a bit which is very strang. Looking at the code i really cannot find out why this is happening. Just asking here in case anyone has had this issue before and found a solution. Thanks!
Hmmm not sure what's going on here. I'd recommend looking at the Code Animator code that I put on the Obra Dinn video. I think that used a better algorithm for text scrolling.
I know this is a very late reply, but just in case anyone else runs into a similar problem, this was my solution. My typewriter effect was done with a coroutine and updated the text using TMPro's maxVisibleCharacters, and added each new character using WaitForSeconds. When that happened, the text wobble effect glitched out, so I think it was happening out of step with Update. So, instead of putting the code from this tutorial in Update, I moved it to its own separate method and then called it every time my coroutine fired (in addition to calling it normally from Update). No more glitching!
is there a way to do this using GPU computing via shaders or custom properties? it feels like using a C# script for a mesh, every vert for every frame would be horrible for CPU calculations
"Just copy it, you don't need to understand this" Uh... I thought the whole point of tutorials was to make me understand at least most of the stuff that was mentioned .-.
it works perfectly on a single scene but when i tried to apply the WordWobble script to another text on a different scene it keeps giving me a null reference exception error on this line textMesh.ForceMeshUpdate(); it would be very helpful if you could help me understand and solve this!
On my first scene where i used the script on a text mesh pro element, i dont get any errors but when i push the button on my screen to switch to the next scene, the text element where i applied the same wordwobble script says that on line 38 object referecne is not set to an instance of an object. I also got the same error for line 26 Line 38: textMesh.ForceMeshUpdate(); Line 26: string s = textMesh.text;
@@Madalaski Yes, i am. i just finished doing a test run of the same situation on a new empty unity project and im not getting that problem anymore. However its still very strange and confusing that i was getting this error in the first place. Im very new to unity so its pretty rough having to deal with these errors. If you have any idea what might be causing it, feel free to let me know as i would still like to find out the reason behind it and what that error even means.
@@justinruane3796 Hey there, sorry I've been swamped today. Your code looks fine and your errors just mean that the textMesh component is somehow being accessed before it's set or doesn't exist in the first place. So, if you're certain that you're using a textmeshpro text object and not a regular text UI element, then the only other explanation is that Update() is being called before Start() when you load the scene. Super weird but easily fixable. Either make textMesh variable public and drag the TMP_Text component into it's field in the inspector or wrap the whole of the Update() function with a if (textMesh != null) { } statement.
Super good tutorial.
I've been struggling with applying text effect in TMP, and this tutorial came in clutch!
It's extremely quick, easy to understand, and straight to the point!
Thank man! You really helped me and others out!
No worries, thanks for enjoying it!
When one legend leaves behind a legacy, more emerge from the beyond! Glad to find a new senpai to tut me thru!
Oh friend, I'm no Brackeys that's for sure. We cannot replace the legends that came before us but we do our best to live up to them.
But hey, if there's anything you'd like me to cover in a tutorial, give me a shout!
@@Madalaski true true! None cud replace em but teaching others the stuff u know is just so humane and humble of y'all imo!
Ps idk what certain topic to point out in Unity but a series on the current ways of networking in it wud be immensely helpful (if it's within the scope of your interests of course)! Anyway keep the Shader tuts flowing in and with it, anything Unity would be welcomed by the majority I guess XD! Great job senpai!
Thank you so much for this tutorial!
*But I would highly recommend skipping spaces:*
for (int i = 0; i < charCount; i++)
{
TMP_CharacterInfo c = textMesh.textInfo.characterInfo[i];
// Skip spaces as they don't have any vertices.
//
// They also have a vertexIndex of zero which causes confusing
// issues with modifications to the first letter.
if (c.character == ' ')
{
continue;
}
...
}
Thank you so much! The tutorial was awesome, but this saved me so much time.
Thanks! I was going to buy a $50 asset from the asset store, but I found this awesome tutorial!
Always happy to help save money!
@@Madalaski I'm trying to do an effect where I display a score counter, and each time a number of the score changes, I play a little animation for that character. The characters that stay the same don't animate.
@@Madalaski Like this game made by me: sullystudios.itch.io/space-all-alone
@@JacksonAcademy1 Aw nice, I bet this'll look sick 😎😎😎
@@Madalaski Wait lol i meant to ask for help, how do I detect if a character has changed
Amazing video, very concise, definitely the best video about this topic out there :)
There is very little documentation about this, and it is very well explained in your video! It has helped me a lot, however it is still difficult for me to implement it by characters separately... You could upload more tutorials and different types of animations when typing texts, it would be very useful!
Thanks for the vid. You got a sub for this.
This tutorial was incredible thank you!
I took the liberty of modifying the code a bit to add the ability to apply this effect to only one part of your whole text! (needed it for my game) and added some quality of life bools :)
(again, I modified Madalaski code so props to him!)
Here is the complete code for anyone that wants it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class RainbowText : MonoBehaviour
{
//Variables for Word Change
[SerializeField] bool applyOnlyToSelection;
[TextArea(5,5)] //make this area bigger if you need to add more lines
[SerializeField] string textToChangeColor;
int specificWordIndex;
//Variables for Text Change
TMP_Text textMesh;
Mesh mesh;
Vector3[] vertices;
List wordIndexes;
List wordLengths;
//Wobbling Variables
Vector3 offset;
[SerializeField] bool wobbleText = false;
[SerializeField] float wobblingSpeed = 1f;
//global Variables
TMP_Text tempTextMesh;
string tempString;
//Color Variables
[SerializeField] float colorSpeed = 2f;
public Gradient rainbow;
// Start is called before the first frame update
void Start()
{
textMesh = GetComponent();
wordIndexes = new List();
wordLengths = new List();
if (applyOnlyToSelection)
{
char[] tempChars = textToChangeColor.ToLower().ToCharArray();
tempString = textMesh.text.ToLower();
for (int i = 0; i < tempString.Length; i++)
{
if(tempString[i].CompareTo(tempChars[0]) == 0) //check if the first letter of the Selection is found
{
for (int j = 1; j < tempChars.Length; j++) //Go over every character in our selection
{
if(tempString[i + j].CompareTo(tempChars[j]) != 0) //if next character is not the character in our selection, go back to the 1st For Loop
{
break;
}
if(j == tempChars.Length - 1) //if every character was correct, We found our selection!!
{
print("Found the word!");
wordIndexes.Add(i);
wordLengths.Add(tempChars.Length);
tempString = textToChangeColor;
return;
}
}
}
//Our selection was not found, Script will deactivate itself because it wont do anything anyways
if (i == tempString.Length - 1) { print("Word not found!"); this.enabled = false; }
}
}
else
{
wordIndexes.Add(0); //wordIndexes needs to start with an index of 0
tempString = textMesh.text;
for (int index = tempString.IndexOf(' '); index > -1; index = tempString.IndexOf(' ', index + 1))
{
wordLengths.Add(index - wordIndexes[wordIndexes.Count - 1]);
wordIndexes.Add(index + 1);
}
wordLengths.Add(tempString.Length - wordIndexes[wordIndexes.Count - 1]);
}
}
// Update is called once per frame
void Update()
{
textMesh.ForceMeshUpdate();
mesh = textMesh.mesh;
vertices = mesh.vertices;
Color[] colors = mesh.colors;
for (int w = 0; w < wordIndexes.Count; w++)
{
int wordIndex = wordIndexes[w];
if(wobbleText) offset = Wobble(Time.time * wobblingSpeed + w);
for (int i = 0; i < wordLengths[w]; i++)
{
TMP_CharacterInfo c = textMesh.textInfo.characterInfo[wordIndex + i];
int index = c.vertexIndex;
if (applyOnlyToSelection) { if (index == 0) continue; } //if there is a blank character, go to the next character instead
colors[index] = rainbow.Evaluate(Mathf.Repeat(Time.time * colorSpeed - vertices[index].x * 0.001f, 1f));
colors[index + 1] = rainbow.Evaluate(Mathf.Repeat(Time.time * colorSpeed - vertices[index + 1].x * 0.001f, 1f));
colors[index + 2] = rainbow.Evaluate(Mathf.Repeat(Time.time * colorSpeed - vertices[index + 2].x * 0.001f, 1f));
colors[index + 3] = rainbow.Evaluate(Mathf.Repeat(Time.time * colorSpeed - vertices[index + 3].x * 0.001f, 1f));
if (wobbleText)
{
vertices[index] += offset;
vertices[index + 1] += offset;
vertices[index + 2] += offset;
vertices[index + 3] += offset;
}
}
}
mesh.vertices = vertices;
mesh.colors = colors;
textMesh.canvasRenderer.SetMesh(mesh);
}
Vector2 Wobble(float time)
{
return new Vector2(Mathf.Sin(time * 3.3f), Mathf.Cos(time * 2.5f));
}
}
This is a really cool solution! Mad props!
I imagine an easier method would be to iterate through the WordInfo information in TextInfo property. Then you could just compare each word in there to the word you're trying to animate rather than having to go character by character. Or if you set the index of the word you want to animate you could go straight there with TextInfo.WordInfo[w].GetWord() but I don't know if that's better for your purposes or not.
@@Madalaski Thank you! That is actually the first route I took (WordInfo[w].GetWord()) but was having trouble getting it to work properly, I definitely think it could be improved with that, I just took this route Im more familiar with, still fairly new to all the possibilities with TMPro :)
@@vimtutorials5234 No worries just figured I'd point it out!
This is really cool! Thank you!
I did notice a bug, though: If the first letter in the text is part of the selection it is skipped.
Still, this is really nice. I think I'll modify it a bit by moving it inside a Coroutine and use it in my current project.
Such a cool effect, looks awesome!
Where is that triangles-in-a-trenchcoat quote from, cracked me up XD
My brain xD
LMAO "A square is just 2 triangles in a trench coat." I like that lol
Great tutorial, looks great :)
Only weird thing is that for some reason the first letter of a paragraph moves WAY more than all of the other ones, only sometimes though...
exactly what I was looking for, great!
Damn, this tutorial was helpful! Thanks a lot :D
By half of the video i checked the speed but i realized its normal :D
Oh wow, what a neat little tutorial. Thanks!
This actually helped me so much, thank you
Nice video, thanks! I was having a trouble making an effect for character because for some reason the first character always seems to take double the effect. If anyone having the same issue the fix is easy: instead of making a looping through the characters of the sentence, I just did a loop through all vertex:
for (int i = 1; i < text.mesh.vertexCount; i++)
{
vertices[i] += Wobble();
}
The rest is the same and it works like a charm. Also, I did a fast test and it seems the loop is always the same size, doesn't matter how long or short your text is.
Hey there pretty sure you need to use int i = 0 at the start of that for loop. Don't forget your zero indexing!
@@Madalaski you are right, I was doing some tests to check the first letter and forgot to go back to 0
New sub here :) amazing content boi
Cheers! Stick around for new content soon!!!!!
Where do we put the script? All the code is written correctly and I also put it on my text that uses TMPro but nothing happens.
Nice video!
Nice comment! =D
Thanks for your tutotial!
I don't even have to type anything and it works !
Thanks thanks thanks!!!
Uhhhhhhh let me yeeeeeeet that code
Yeeeeeeeeeeeeeet
Yeet away. Yeet away.
good one
do you know a way that I can call these effects using tags in textmeshpro... would be cool to have specific words wobble instead of the whole objects string...
Same as in words example. Find your word indexes and do stuff with it.
This is a very good straight to the point tutorial! However I spotted a weird artifact appearing in the very center of the TMP asset (at the position of the TMP [T] gizmo). You can sort of see it at 1:41 at the top left corner of the 'm' in "some". When inspected closely it's a square mesh wobbling with the text, it doesn't matter if there's any text near the area, just appears at the very center.
Sometimes this artifact disappears when I add a few more characters and sometimes it itself shows as a blob of characters... Any ideas what might cause this? Thanks!
Hmmm, if I think I noticed this problem a couple months later but I can't remember the exact suction. I believe it has something to do with a character being added to overall text but the new character didn't have it's new vertices set, creating an artifact. Might be worth to create a buffer zone of some kind. One of my later videos has code for my Code Animator software in the description. I believe that uses a better algorithm if you want to check that out.
If you're still encountering this issue, I've just found a solution, so even if you've fixed it on your own I'll leave it here for people who come after ^u^
It looks like this artifact is where "extra" characters are stored. Instead of creating and destroying a bunch of meshes, the engine just creates a mesh when needed and stores it very very small when it's not being used. However when messing with the vertices, you manually make these "extra" stored meshes visible by giving them scale. Accessing the vertices on a character-by-character basis instead of vertex-by-vertex basis should fix the issue, since you aren't accessing the extra stored meshes this way.
U should livestream the creation of ur game and then edit them into devlogs 🤷♂️
Sadly I do not have the internet connection for streaming at the moment ;(
Plus that would spoil the surprises... ;)
@@Madalaski understandable, have a nice day
Great video, straight to the point!
But I didn't get the part at 1:38...
I am using a non-UI, world space TextMeshPro component and the mesh gets all deformed. Obviously there is no canvas renderer for that, but there's also no "SetMesh" method. What am I supposed to assign in order for the modified mesh to work with world space text?
i'm having this issue, did you figure it out?
ok so these text effects are AMAZING!
there is one niche problem that i am having though,
when the text is scrolling through with the typewriter effect,
the wobble and colour effects seem to jitter a bit which is very strang.
Looking at the code i really cannot find out why this is happening.
Just asking here in case anyone has had this issue before and found a solution.
Thanks!
Hmmm not sure what's going on here. I'd recommend looking at the Code Animator code that I put on the Obra Dinn video. I think that used a better algorithm for text scrolling.
I know this is a very late reply, but just in case anyone else runs into a similar problem, this was my solution. My typewriter effect was done with a coroutine and updated the text using TMPro's maxVisibleCharacters, and added each new character using WaitForSeconds. When that happened, the text wobble effect glitched out, so I think it was happening out of step with Update.
So, instead of putting the code from this tutorial in Update, I moved it to its own separate method and then called it every time my coroutine fired (in addition to calling it normally from Update). No more glitching!
where do i even put the script? its not as a component to the text layer... I am too nooby to get this help
Awesome
I couldn't find it on Unity Store??
Find what? TextMeshPro? It's in Packages, already comes with Unity.
is there a way to do this using GPU computing via shaders or custom properties?
it feels like using a C# script for a mesh, every vert for every frame would be horrible for CPU calculations
Possibly! But you'd definitely have to feed in some kind of information about which characters are being affected when :)
text just move 1 frame and stops, script is the same as yours
murican grades are 1-12.
"Just copy it, you don't need to understand this"
Uh... I thought the whole point of tutorials was to make me understand at least most of the stuff that was mentioned .-.
COOL TEXT EFFECTS IN UNITY: How to smash your GPU and explode your draw calls.
it works perfectly on a single scene but when i tried to apply the WordWobble script to another text on a different scene it keeps giving me a null reference exception error on this line
textMesh.ForceMeshUpdate();
it would be very helpful if you could help me understand and solve this!
Are you sure the text ur applying the script to is using TextMeshPro?
@@Madalaski yes, i am
Here is the code if you like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class WordWobble : MonoBehaviour
{
TMP_Text textMesh;
Mesh mesh;
Vector3[] vertices;
List wordIndexes;
List wordLengths;
public Gradient rainbow;
// Start is called before the first frame update
void Start()
{
textMesh = GetComponent();
wordIndexes = new List { 0 };
wordLengths = new List();
string s = textMesh.text;
for (int index = s.IndexOf(' '); index > -1; index = s.IndexOf(' ', index + 1))
{
wordLengths.Add(index - wordIndexes[wordIndexes.Count - 1]);
wordIndexes.Add(index + 1);
}
wordLengths.Add(s.Length - wordIndexes[wordIndexes.Count - 1]);
}
// Update is called once per frame
void Update()
{
textMesh.ForceMeshUpdate();
mesh = textMesh.mesh;
vertices = mesh.vertices;
Color[] colors = mesh.colors;
for (int w = 0; w < wordIndexes.Count; w++)
{
int wordIndex = wordIndexes[w];
Vector3 offset = Wobble(Time.time + w);
for (int i = 0; i < wordLengths[w]; i++)
{
TMP_CharacterInfo c = textMesh.textInfo.characterInfo[wordIndex + i];
int index = c.vertexIndex;
colors[index] = rainbow.Evaluate(Mathf.Repeat(Time.time + vertices[index].x * 0.001f, 1f));
colors[index + 1] = rainbow.Evaluate(Mathf.Repeat(Time.time + vertices[index + 1].x * 0.001f, 1f));
colors[index + 2] = rainbow.Evaluate(Mathf.Repeat(Time.time + vertices[index + 2].x * 0.001f, 1f));
colors[index + 3] = rainbow.Evaluate(Mathf.Repeat(Time.time + vertices[index + 3].x * 0.001f, 1f));
vertices[index] += offset;
vertices[index + 1] += offset;
vertices[index + 2] += offset;
vertices[index + 3] += offset;
}
}
mesh.vertices = vertices;
mesh.colors = colors;
textMesh.canvasRenderer.SetMesh(mesh);
}
Vector2 Wobble(float time)
{
return new Vector2(Mathf.Sin(time * 3.3f), Mathf.Cos(time * 2.5f));
}
}
On my first scene where i used the script on a text mesh pro element, i dont get any errors but when i push the button on my screen to switch to the next scene, the
text element where i applied the same wordwobble script says that on line 38 object referecne is not set to an instance of an object. I also got the same error for line 26
Line 38:
textMesh.ForceMeshUpdate();
Line 26:
string s = textMesh.text;
@@Madalaski Yes, i am. i just finished doing a test run of the same situation on a new empty unity project and im not getting that problem anymore. However its still very strange and confusing that i was getting this error in the first place. Im very new to unity so its pretty rough having to deal with these errors. If you have any idea what might be causing it, feel free to let me know as i would still like to find out the reason behind it and what that error even means.
@@justinruane3796 Hey there, sorry I've been swamped today. Your code looks fine and your errors just mean that the textMesh component is somehow being accessed before it's set or doesn't exist in the first place. So, if you're certain that you're using a textmeshpro text object and not a regular text UI element, then the only other explanation is that Update() is being called before Start() when you load the scene. Super weird but easily fixable. Either make textMesh variable public and drag the TMP_Text component into it's field in the inspector or wrap the whole of the Update() function with a if (textMesh != null) { } statement.