- Home /
Question by
Phoenixfck · May 12, 2015 at 01:10 PM ·
c#getcomponentgameobject.find
Change the value of string in other class by triggering collision (c#)
I have a 3D text with the following script (txtcontrol)
public class txtcontrol : MonoBehaviour {
public string txt = "1";
public void Start(){
TextMesh t = (TextMesh)gameObject.GetComponent (typeof(TextMesh));
t.text = txt;
and an empty gameobject with box collider. I want to change the value of string "txt" in "txtcontrol" script by triggering this box collider with "human1" script
public class human1 : MonoBehaviour {
public string txt2 = "2";
GameObject control = GameObject.Find("dialogtxt");
void change(){
control = gameObject.GetComponent<txtcontrol>().txt = txt2;
but I get an error "Cannot implicitly convert type 'string' to 'UnityEngine.GameObject'. This is my first project in unity and i have almost no experience in programming.
Comment
in change you shouls dirst assign ( as you correctly did) your gameobject txtcontrol to the variable control. Then in the next line try to do control.text = whatev
Let me know if it works , maybe it wont but I have other ideas
Answer by xortrox · May 12, 2015 at 02:06 PM
You are trying to assign a string to your control variable which is a GameObject.
Try this:
txtcontrol control;
control = gameObject.GetComponent();
control.txt = txt2;