Issue with assigning text in a text box via script

I have tried to set a variable in a script two different ways however I still get the same issue for both telling me that “initializer cannot reference the monostatic field, method etc.”

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Health : MonoBehaviour
{
    public Text text;
    public int PHealth = 4;
    void start()
    {
       
     

        if (text.text) text.text = PHealth.ToString(); //assign health value to text
   
    }

}
public class Health : MonoBehaviour
{
    public Text text;
    public int PHealth = 4;
    GameObject CHealth = GameObject.Find("Health");  // get reference for health
    Health Chealth = CHealth.GetComponent<Health>();

    void start()
    {
      
    

        if (text.text) text.text = PHealth.ToString(); //assign health value to text
  
    }

}

the text has been set to the Health text in the inspector.

1 Like

Hi SoulbladeSBA,

Change the name of your Text variable to something like “myText”. You can’t have a Text variable with the name “text”. It confuses the program.

Hope this helps!

thank you it helped but now I get an error stating that it cannot implicitly convert ‘string’ to ‘bool’ which has lost me completely.

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Health : MonoBehaviour
{
    public Text health;
    public int PHealth = 4;
    //GameObject CHealth = GameObject.Find("Health");  // get reference for health
    //Health Chealth = CHealth.GetComponent<Health>();

    void start()
    {
       
     

        if (health.text) health.text = PHealth.ToString(); //assign health value to text
   
    }

}

if (health) health.text = PHealth.ToString(); //assign health value to text

thank you, that removes the error however nothing appears in the text box

sorry fixed, issue was with the function being “void start” instead of what it should have been “void Start”