Reaching a Script of GameObject List?

Hello. I want to ask some questions about gameObject List. I have a gameObject list, and I want to reach its attached script and call a function from it. Can we use the code like this?

GO_Harf goh0 = (GO_Harf)ananList*.GetComponent(“GO_Harf”);*
GO_Harf is the name of script that attached to a game object that from the List that named ananList. “i” is zero. Thus it should reach the script of the gameObject that is in the list but it doesn’t work. Why?
Basically, I’m asking this: “How can I reach to a script of a Game Object that is in Game Object List?”
Sorry for bad description.
Thanks.

I personally prefer to use the generic get component method, but your does not look wrong.

GO_Harf goh0 = ananList[i].GetComponent<GO_Harf>();

If after that call goh0 is null, the game object simply has no GO_Harf component in it. If it is in a child of that game object, you may use GetComponentInChildren. Check the list with the game objects and see if the component really exists.

I checked now and it exists. Otherwise, it should give “NullException” error maybe?

If the game object has not been found and you try to execute something in that component then it will return null exception, yes. It’s good practice to do null checks though.

But, it doesn’t give NullException error. :frowning: it doesn’t give any errors.

What cjow said correctly is that if you perform any call on the found component, like

Debug.Log (goh0.name);

you are going to get a null reference exception. If that doesn’t happen, the component was found!

Oh, but I don’t get an error. I also tried Debug.Log(goh0.name); and it returned as “HarfPrefab(Clone)”. Then there is no error in that line. Maybe I can give the project file? I was trying to make a vector font that prints on the screen with typewriter effect. Typewriter effect makes confusion but I want to program it. All professional games uses typewriter effect I think and I like it too.

Whatever, maybe it was about the function that I want to call. Its name is “YaratmaFonk” and it is in the gameObject’s script(GO_Harf) that I want to reach. I made something like this:

ananList.Add(Instantiate(anan, Vector3.zero, Quaternion.identity) as GameObject);
GO_Harf goh0 = (GO_Harf)ananList*.GetComponent(“GO_Harf”);*
goh0.YaratmaFonk(offsetX, offsetY, 0);
Maybe there was something wrong with other part of the code? I give the project if anyone wants to look at it.
Thanks again.
1022982–37945–$Deneme.rar (536 KB)

Had a look at your project, but unfortunately I don’t understand your code due to the language. I see some kind of type writer effect, but I have no idea what you would like to achieve. Please explain what is going wrong and what should actually happen. Otherwise it’s about impossible for us to help.

Oh what do you mean by “language”? Because of the C# or I wrote confusing code? :frowning: Oh I get it, I wrote the language in my native language. Sorry about that. Thanks for you effort by the way. I will explain what is going on.

I was trying to print vector font to the screen using GUI. I made all the letters and then I made a gameObject. There are two scripts on the project. “YaziKodu” is sending some information to GO_Harf script. GO_Harf script is attached to a letter prefab. YaziKodu makes the letter prefab appear on the screen with Instantiate, then I call a function from GO_Harf in YaziKodu and send the information of letter’s position and code(for example code of letter A is 0 because it is first letter). Also if “\n” character used or nax letter on a line is 25, yOffset increased by one and xOffset set zero. There are some time code that makes print the letter in time.

Whatever, my description is confusing I think. I will say what’s going on simple. For example, I want to print “Hello!” to the screen. The size of the letters are for example 20x20. I will instantiate six characters but when I instantiate the first character, its xOffset increased by one. GUI takes the offset data and multiples the offset data with size of the letter. First it will print to the x=0 the letter(xOffset is 0, size is 20, 0x20=0). Then xOffset is increased by one. Then now it will be 1x20, and it will print to the x=20. It goes like x=40, x=60 until sixth character. There are a function that sets the position and code of the letter that going to be printed. I reach to function and send to the datas of the letters.

Here’s the meaning of words:
yazi: text
satir: line
harf: letter
islem: operation
zaman: time
sayac: counter
anan: it has a meaning but it is random, just a word, I don’t want to say its meaning :smile:.
harfDegiskeni: letter variable
also yaziKodu means letter code.

Sorry for confusing text, I wrote it fast as I can.

Let’s face it, if we knew it, you would be banned from the forum :wink: !

I’ll see what I can do.

I am pretty sure you would have found the error on your own, if you had made the code structure a little more readable. You have those huge switch code blocks. If you think they are really needed, I would propose you to move them into a separate method. Like that it becomes a lot easier to see what is going on.

The error happens in YaziKodu:

if(satirIslem == 0){
	offsetX=0;
	offsetY++;
}

The inner part of the code block is always executed. You can try it out. Just place some Debug.Log (Time.realtimeSinceStartup); in there. As a consequence, offsetX is always reset to zero.

I don’t want to get banned from the forum. :smile: Actually it wasn’t bad. It just means “your momma”. :smile: I wrote it like that because I was bored. :smile:

It works now. Instead of this:

if(satirIslem == 0){
offsetX=0;
offsetY++;
}

I wrote this:

if(offsetX < 24){
offsetX=0;
offsetY++;
}

Actually it didn’t work again but I found some mistakes in the code and fixed them as you said, I found other mistakes on my own. Then it worked. By the way, It was not readable because I was thinking like “OK, I should make the code first, I will make it readable after writing a working code”. Looks like I was wrong a bit. I accept I am a newbie coder, I can’t take education about programming, I wish I could. I am open to your suggestions, :stuck_out_tongue: I don’t like this huge switch block too. I can’t believe I made a stupid module operation, looks like my math is bad. I learnt that if the code don’t work as you want and it don’t give errors, maybe you can check up other parts. :slight_smile: Whatever, it works, you helped me so much, thanks for your effort! Thanks to cjow too. Here is a caps:

http://imgim.com/print.jpg

By the way, can you see the huge amount of gameObject in the Hierarchy panel? It looks so bad. Suddenly I found a solution, but I don’t think it would help after I thinking on it a while. It was about adding the letter to other variable(for example I want to print “hello!” then I give the letters to another string variable in time and then I print it that variable with GUI but new lines won’t work, maybe I can print lines as seperate strings.)I need a solution about fixing that big amount of gameObject and seperated letter graphics now. :frowning:

I am glad it was useful!

Unfortunately I don’t have the time to help you with the other problem.

Oh OK then, forget about it. Actually in that case, you helped me really much although you don’t have time. Thanks. I should find out myself.

Couldn’t resist.

At the moment each character is an object of type GO_Harf. I would try to keep that class, but make sure it doesn’t inherit MonoBehaviour and there will certainly be no more Instantiate call, so you would need to create instances with new. Instead there should be another class that manages the rendering and knows all the character textures. That manager inherits MonoBehaviour. It certainly needs a list with all the characters (List of Harf).

Not sure if that is useful for you.

I don’t get it the suppose of this way. Why we use Class without MonoBehaviour instead of Instantiate and a Class that inherits MonoBehaviour? I thought of a new way that reduce Instantiated object number to 1, 2 or 3. I tried to describe it here but it is too long. But if you want, I can code it and give the project file here. You can give me some advices to reduce the big switch code blocks also. :smile:

You can also just post the scripts. Like that, others may have a look at it too. But that’s up to you.

Oh I’m trying to write the code since 3-4 hours, it gets boring I can’t take it anymore I’ve quitted writing. :smile: Maybe if I continue to writing the code, I will success but I don’t want to write it over and over, sorry. And I hate that huge switch block. I find ideas, I mean maybe I can do bitmap fonts with just one script but I can’t write it because I get tired and confused quickly.

Here’s the unfinished code. It doesn’t give error but it doesn’t print fonts either. And this is the simplest form. It was splitted into two scripts once.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TextController : MonoBehaviour {
	public Texture2D[] theFont;

	private string text1before;
	private string text1after;
	private int arrayCounter;
	private int offset;
	private float timeCounter;
	private int guiCounter;

	void Start() {
		text1before = "HOW ARE YOU?";
		text1after = null;
		arrayCounter = 0;
		timeCounter = 0;
		guiCounter = 0;
		offset = 0;
	}

	void Update() {
		timeCounter += Time.deltaTime;

		if(timeCounter >= 0.1) {
			if(arrayCounter < text1before.Length){
				switch(text1before[arrayCounter]){
					case 'A':
						text1after = text1after + 'A';
						break;
					case 'B':
						text1after = text1after + 'B';
						break;
					case 'C':
						text1after = text1after + 'C';
						break;
					case '\u00C7':
						text1after = text1after + '\u00C7';
						break;
					case 'D':
						text1after = text1after + 'D';
						break;
					case 'E':
						text1after = text1after + 'E';
						break;
					case 'F':
						text1after = text1after + 'F';
						break;
					case 'G':
						text1after = text1after + 'G';
						break;
					case '\u011E':
						text1after = text1after + '\u011E';
						break;
					case 'H':
						text1after = text1after + 'H';
						break;
					case 'I':
						text1after = text1after + 'I';
						break;
					case '\u0130':
						text1after = text1after + '\u0130';
						break;
					case 'J':
						text1after = text1after + 'J';
						break;
					case 'K':
						text1after = text1after + 'K';
						break;
					case 'L':
						text1after = text1after + 'L';
						break;
					case 'M':
						text1after = text1after + 'M';
						break;
					case 'N':
						text1after = text1after + 'N';
						break;
					case 'O':
						text1after = text1after + 'O';
						break;
					case '\u00D6':
						text1after = text1after + '\u00D6';
						break;
					case 'P':
						text1after = text1after + 'P';
						break;
					case 'Q':
						text1after = text1after + 'Q';
						break;
					case 'R':
						text1after = text1after + 'R';
						break;
					case 'S':
						text1after = text1after + 'S';
						break;
					case '\u015E':
						text1after = text1after + '\u015E';
						break;
					case 'T':
						text1after = text1after + 'T';
						break;
					case 'U':
						text1after = text1after + 'U';
						break;
					case '\u00DC':
						text1after = text1after + '\u00DC';
						break;
					case 'V':
						text1after = text1after + 'V';
						break;
					case 'W':
						text1after = text1after + 'W';
						break;
					case 'X':
						text1after = text1after + 'X';
						break;
					case 'Y':
						text1after = text1after + 'Y';
						break;
					case 'Z':
						text1after = text1after + 'Z';
						break;
					case '0':
						text1after = text1after + '0';
						break;
					case '1':
						text1after = text1after + '1';
						break;
					case '2':
						text1after = text1after + '2';
						break;
					case '3':
						text1after = text1after + '3';
						break;
					case '4':
						text1after = text1after + '4';
						break;
					case '5':
						text1after = text1after + '5';
						break;
					case '6':
						text1after = text1after + '6';
						break;
					case '7':
						text1after = text1after + '7';
						break;
					case '8':
						text1after = text1after + '8';
						break;
					case '9':
						text1after = text1after + '9';
						break;
					case '.':
						text1after = text1after + '.';
						break;
					case '\'':
						text1after = text1after + '\'';
						break;
					case ',':
						text1after = text1after + ',';
						break;
					case '!':
						text1after = text1after + '!';
						break;
					case '?':
						text1after = text1after + '?';
						break;
					case ' ':
						text1after = text1after + ' ';
						break;
				default:
					text1after = text1after + '?';
					break;
				}

			} 
			arrayCounter++;
			timeCounter = 0;
		}
	}
	
	void OnGUI(){
		if(text1after != null){
		if(guiCounter < text1after.Length){
			switch(text1after[guiCounter]){
				case 'A':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'B':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[1]);
					break;
				case 'C':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '\u00C7':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'D':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'E':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'F':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'G':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;		
				case '\u011E':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'H':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'I':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);	
					break;
				case '\u0130':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'J':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'K':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'L':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'M':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'N':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'O':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '\u00D6':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'P':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'Q':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'R':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'S':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '\u015E':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'T':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'U':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '\u00DC':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'V':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'W':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'X':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'Y':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case 'Z':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '0':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '1':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '2':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '3':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '4':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '5':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '6':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '7':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '8':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '9':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '.':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '\'':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case ',':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '!':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case '?':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
				case ' ':
					GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
					break;
			default:
				GUI.DrawTexture(new Rect(offset*24, 54, 162, 600), theFont[0]);
				break;
			}
		} 
		guiCounter++;
		offset++;
		}
	}
}

As I already wrote, the whole switch makes your code unnecessarily hard to understand. I just took your code, removed a lot of variables, moved the switch into a new method and made a few changes that should resolve the issues. Just ask if you have any questions. but have look at the code with the switch statement moved into a separate method! One view and you already have a rough idea about what is going on!

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class TextController : MonoBehaviour {
	public Texture2D[] theFont;
 
	private string text;
	private int visibleCharactersCounter;
	private float timeCounter;
 
	void Start() {
		text = "HOW ARE YOU?";
		visibleCharactersCounter = 0;
		timeCounter = 0.0f;
	}
 
	void Update() {
		timeCounter += Time.deltaTime;
		if(timeCounter >= 0.1f) {
			
				// Make exactly one more character visible.
			visibleCharactersCounter++;
			timeCounter = 0.0f;
			
				// But make sure that the number is not too big.
			if (visibleCharactersCounter > text.Length) {
				visibleCharactersCounter = text.Length;
			}
		}
	}
   
	void OnGUI(){
		
			// Draw each visible character.
		for (int i = 0; i < visibleCharactersCounter; i++) {
			GUI.DrawTexture(new Rect(i*24, 54, 162, 600), TextureFromCharacter (text[i]));
		}
	}
	
	private Texture2D TextureFromCharacter(char character) {
		
			// The whole switch stuff is now in here. There would probably be
			// more elegant solutions for that.
			// I stopped to increase the at character 'K'.
	
		switch(character){
			case 'A':
				return (theFont[0]);
			case 'B':
				return (theFont[1]);
			case 'C':
				return (theFont[2]);
			case '\u00C7':
				return (theFont[3]);
			case 'D':
				return (theFont[5]);
			case 'E':
				return (theFont[6]);
			case 'F':
				return (theFont[7]);
			case 'G':
				return (theFont[8]);	 
			case '\u011E':
				return (theFont[9]);
			case 'H':
				return (theFont[10]);
			case 'I':
				return (theFont[11]);
			case '\u0130':
				return (theFont[12]);
			case 'J':
				return (theFont[13]);
			case 'K':
				return (theFont[14]);
			case 'L':
				return (theFont[0]);
			case 'M':
				return (theFont[0]);
			case 'N':
				return (theFont[0]);
			case 'O':
				return (theFont[0]);
			case '\u00D6':
				return (theFont[0]);
			case 'P':
				return (theFont[0]);
			case 'Q':
				return (theFont[0]);
			case 'R':
				return (theFont[0]);
			case 'S':
				return (theFont[0]);
			case '\u015E':
				return (theFont[0]);
			case 'T':
				return (theFont[0]);
			case 'U':
				return (theFont[0]);
			case '\u00DC':
				return (theFont[0]);
			case 'V':
				return (theFont[0]);
			case 'W':
				return (theFont[0]);
			case 'X':
				return (theFont[0]);
			case 'Y':
				return (theFont[0]);
			case 'Z':
				return (theFont[0]);
			case '0':
				return (theFont[0]);
			case '1':
				return (theFont[0]);
			case '2':
				return (theFont[0]);
			case '3':
				return (theFont[0]);
			case '4':
				return (theFont[0]);
			case '5':
				return (theFont[0]);
			case '6':
				return (theFont[0]);
			case '7':
				return (theFont[0]);
			case '8':
				return (theFont[0]);
			case '9':
				return (theFont[0]);
			case '.':
				return (theFont[0]);
			case '\'':
				return (theFont[0]);
			case ',':
				return (theFont[0]);
			case '!':
				return (theFont[0]);
			case '?':
				return (theFont[0]);
			case ' ':
				return (theFont[0]);
			default:
				return (theFont[0]);
		}
	}
}

Wow now I tried this, and it worked well. Thanks for the help. It is a less confusing script than mine. I was confused as I say “oh I can use for loop but if I use it, one letter will be invisible at one frame”. If you don’t understand what I mean, don’t worry, I was confused. :smile: Whatever, I have to ask some questions:
1-Which University Department did you study?
2-Can I make the font as font sheet? That font list looks messy. I heard, DrawTexture can’t set the offset of texture, because it is not a material.
3-Thanks for your help, how did you write it so quickly. :smile: