Changing a Prefab's Text component seems to be broken.
Hi, I'm very new to Unity/programming, and for the life of me I can't figure out what's wrong here.
I wrote a script to generate a map (very basic one) of different terrain types.
One of the "terrains" is a city, that, when Instantiated, pulls a city name from a random list, and edits both the GameObject's name and a text box to have the name it selected.
When the script executes upon instantiation, sometimes the GameObject's name and the text box are different names, and sometimes the text box isn't changed at all (this seems to be random).
This script is attached to the prefab of the city, while the map creation script is elsewhere (and it works fine as far as I can tell, and is unrelated to changing the text).
Any ideas as to why this very simple script won't do what I need it to?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine.UI;
using System.IO;
public class NameProcedure : MonoBehaviour
{
public TextAsset capitals; // Standard text file
// Start is called before the first frame update
void Start()
{
#if UNITY_EDITOR
string path = AssetDatabase.GetAssetPath(capitals);
#else
string path = (Application.dataPath + "/capitals.txt");
#endif
int fileLength = File.ReadAllLines(path).Length; // How long is the file?
string[] names = File.ReadAllLines(path); // Each line is given to an array of city names.
int rand = Random.Range(1, fileLength + 1); // Generate number between 1 and the length of the file (Random.Range(int, int) is max exclusive, hence the +1)).
string chosenName = names[rand]; // Assign an string, chosenName, a randomly... chosen name.
string cityName = GameObject.Find("CityName").GetComponent<Text>().text; // Find the original text information of the city name (from a Text component) for debug purposes.
Debug.Log(chosenName); // Tell me what the random name is.
Debug.Log(cityName); // Tell me what the original name is.
if (GameObject.Find("CityName") == null) // If, for whatever reason, the "CityName" text component can't be found, log an error.
{
Debug.LogError("City not found!");
}
else
{
transform.name = chosenName; // The city prefab this script is attached to should now be named whatever name was chosen randomly.
Debug.Log(transform.name); // Make sure this is true.
GameObject.Find("CityName").GetComponent<Text>().text = chosenName; // Set the text of the city name to the new chosen name.
Debug.Log(GameObject.Find("CityName").GetComponent<Text>().text); // Make sure it worked.
}
}
}
$$anonymous$$aybe check for run time (red dot) errors. Those will "crash" the current script being run, skipping everything past, but otherwise it keeps running, so they're easy to miss. Also try testing GetComponent(Text).text by itself -- it should be fine. That should reassure you that the problem is somewhere else.
Answer by TheAmerican · Jan 12, 2020 at 06:29 AM
I fixed it by moving the script from the parent object (the city object) to the child I wanted to edit (the text box) and shortening the GetComponent parts (thanks to Owen Reynolds for the suggestion!).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine.UI;
using System.IO;
public class NameProcedure : MonoBehaviour
{
public TextAsset capitals; // Standard text file
// Start is called before the first frame update
void Start()
{
#if UNITY_EDITOR
string path = AssetDatabase.GetAssetPath(capitals);
#else
string path = (Application.dataPath + "/capitals.txt");
#endif
int fileLength = File.ReadAllLines(path).Length; // How long is the file?
string[] names = File.ReadAllLines(path); // Each line is given to an array of city names.
int rand = Random.Range(1, fileLength + 1); // Generate number between 1 and the length of the file (Random.Range(int, int) is max exclusive, hence the +1)).
string chosenName = names[rand]; // Assign an string, chosenName, a randomly... chosen name.
string cityName = GetComponent<Text>().text; // Find the original text information of the city name (from a Text component) for debug purposes.
Debug.Log(chosenName); // Tell me what the random name is.
Debug.Log(cityName); // Tell me what the original name is.
if (GameObject.Find("CityName") == null) // If, for whatever reason, the "CityName" text component can't be found, log an error.
{
Debug.LogError("City not found!");
}
else
{
transform.name = chosenName; // The city prefab this script is attached to should now be named whatever name was chosen randomly.
Debug.Log(transform.name); // Make sure this is true.
GetComponent<Text>().text = chosenName; // Set the text of the city name to the new chosen name.
}
}
}
Ah -- that last line with GetCompoennt all by itself was probably the problem. You can write code that always uses a1.transform, a2.GetComponent and so on, and it works fine. But in Unity you can also do things to "yourself" -- simply "transform" and "GetComponent". That can be confusing.