Update Method of the Component aren't Working...

Hello, I had a problem last night when I try to write some script. The problem is, there are two script files, let’s call them like script1 and script2. script1 is attached to a GameObject that is in the scene. script2 is in a prefab(let’s call this as thePrefab). I Instantiate thePrefab with script1 and call a method of its. This method changes the value of a variable. Whatever, starting value of the variable is zero, I put Debug.Log(theVariable); to Update method of script2. This will write zero to the console, right? Umm, but when I try to change the value with the method, it still writes 0. Looks like Update method is called once. The new method value is not printed. When I try to add “Update();” to the method, it just calls it twice. How I can call Update method in the script2 in every frame? Thank you.

I think I can’t describe the problem. I will explain it simple as I can. First, I will give the two script(I simplified both):

using UnityEngine;
using System.Collections;

public class title_script : MonoBehaviour {
	
public GameObject textObj;

	void Start () {
		Instantiate(textObj, Vector3.zero, Quaternion.identity);
		font_script text1 = (font_script)textObj.GetComponent("font_script");
		text1.AttachFunc("Türkçe");
}
	}
public class font_script : MonoBehaviour {
	
	private string txt;
private float time;

void Update () {
		if(typeWEff)	
		{
			time += Time.deltaTime;
		}
		Debug.Log(txt);
	}

public void AttachFunc(string sentText) {
		txt = sentText;
		Debug.Log(txt);
	}
}

The problem is, in the AttachFunc, sentText is attached to the txt variable, but in Update function, txt is still null. I don’t understand. I put Debug.Log(txt); in the AttachFunc, and it returns the attached string value actually. Looks like, it’s not null. So what is the problem? Thanks again and sorry.

sorry, i’m not sure if i understand you words, but in the first script you should access the instantiated object instead of the prefab:

GameObject go = Instantiate(textObj, Vector3.zero, Quaternion.identity) as GameObject;
font_script text1 = (font_script)go.GetComponent<font_script>();
text1.AttachFunc(“Türkçe”);

that should do it.

Why cast the GetCOmponent?

font_script text1 = go.GetComponent<font_script>();

works fine :smile:

ahm, yes… i just mindlessly copied that :stuck_out_tongue:

It works! Thank you guys!!! And sorry for the trouble. :frowning: